2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
35 { { 0x00010001, 0x00010001 } },
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static struct enum_device_entry
44 char interface_name[100];
45 char device_name[100];
46 const GUID *device_guid;
51 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
53 &IID_IDirect3DTnLHalDevice,
58 "WINE Direct3D7 Hardware acceleration using WineD3D",
60 &IID_IDirect3DHALDevice,
65 "WINE Direct3D7 RGB Software Emulation using WineD3D",
67 &IID_IDirect3DRGBDevice,
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
75 ddraw_null_wined3d_object_destroyed,
78 static inline IDirectDrawImpl *impl_from_IDirectDraw(IDirectDraw *iface)
80 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw_iface);
83 static inline IDirectDrawImpl *impl_from_IDirectDraw2(IDirectDraw2 *iface)
85 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw2_iface);
88 static inline IDirectDrawImpl *impl_from_IDirectDraw3(IDirectDraw3 *iface)
90 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw3_iface);
93 static inline IDirectDrawImpl *impl_from_IDirectDraw4(IDirectDraw4 *iface)
95 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw4_iface);
98 static inline IDirectDrawImpl *impl_from_IDirectDraw7(IDirectDraw7 *iface)
100 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw7_iface);
103 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
105 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
108 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
110 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
113 static inline IDirectDrawImpl *impl_from_IDirect3D3(IDirect3D3 *iface)
115 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D3_iface);
118 static inline IDirectDrawImpl *impl_from_IDirect3D7(IDirect3D7 *iface)
120 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D7_iface);
123 /*****************************************************************************
125 *****************************************************************************/
127 /*****************************************************************************
128 * IDirectDraw7::QueryInterface
130 * Queries different interfaces of the DirectDraw object. It can return
131 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
132 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
134 * The returned interface is AddRef()-ed before it's returned
136 * Used for version 1, 2, 4 and 7
139 * refiid: Interface ID asked for
140 * obj: Used to return the interface pointer
143 * S_OK if an interface was found
144 * E_NOINTERFACE if the requested interface wasn't found
146 *****************************************************************************/
147 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
149 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
151 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
153 /* Can change surface impl type */
154 EnterCriticalSection(&ddraw_cs);
156 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
161 LeaveCriticalSection(&ddraw_cs);
162 return DDERR_INVALIDPARAMS;
165 /* Check DirectDraw Interfaces */
166 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
167 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
170 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
172 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
174 *obj = &This->IDirectDraw4_iface;
175 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
177 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
179 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
180 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
182 LeaveCriticalSection(&ddraw_cs);
183 return E_NOINTERFACE;
185 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
187 *obj = &This->IDirectDraw2_iface;
188 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
190 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
192 *obj = &This->IDirectDraw_iface;
193 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
197 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
198 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
199 * who had this idea and why. The older interfaces can query and IDirect3D version
200 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
201 * and messy to implement with the common creation function, so it has been left out here.
203 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
204 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
205 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
206 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
208 /* Check the surface implementation */
209 if(This->ImplType == SURFACE_UNKNOWN)
211 /* Apps may create the IDirect3D Interface before the primary surface.
212 * set the surface implementation */
213 This->ImplType = SURFACE_OPENGL;
214 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
216 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
218 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
219 ERR(" (%p) You may want to contact wine-devel for help\n", This);
220 /* Should I assert(0) here??? */
222 else if(This->ImplType != SURFACE_OPENGL)
224 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
225 /* Do not abort here, only reject 3D Device creation */
228 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
230 This->d3dversion = 1;
231 *obj = &This->IDirect3D_iface;
232 TRACE(" returning Direct3D interface at %p.\n", *obj);
234 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
236 This->d3dversion = 2;
237 *obj = &This->IDirect3D2_iface;
238 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
240 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
242 This->d3dversion = 3;
243 *obj = &This->IDirect3D3_iface;
244 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
246 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
248 This->d3dversion = 7;
249 *obj = &This->IDirect3D7_iface;
250 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
253 /* Unknown interface */
256 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
257 LeaveCriticalSection(&ddraw_cs);
258 return E_NOINTERFACE;
261 IUnknown_AddRef( (IUnknown *) *obj );
262 LeaveCriticalSection(&ddraw_cs);
266 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
268 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
270 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
272 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
275 static HRESULT WINAPI ddraw3_QueryInterface(IDirectDraw3 *iface, REFIID riid, void **object)
277 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
279 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
281 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
284 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
286 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
288 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
290 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
293 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
295 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
297 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
299 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
302 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
304 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
306 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
311 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
313 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
315 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
317 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
320 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
322 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
324 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
326 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
329 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
331 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
333 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
335 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
338 /*****************************************************************************
339 * IDirectDraw7::AddRef
341 * Increases the interfaces refcount, basically
343 * DDraw refcounting is a bit tricky. The different DirectDraw interface
344 * versions have individual refcounts, but the IDirect3D interfaces do not.
345 * All interfaces are from one object, that means calling QueryInterface on an
346 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
347 * IDirectDrawImpl object.
349 * That means all AddRef and Release implementations of IDirectDrawX work
350 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
351 * except of IDirect3D7 which thunks to IDirectDraw7
353 * Returns: The new refcount
355 *****************************************************************************/
356 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
358 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
359 ULONG ref = InterlockedIncrement(&This->ref7);
361 TRACE("%p increasing refcount to %u.\n", This, ref);
363 if(ref == 1) InterlockedIncrement(&This->numIfaces);
368 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
370 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
371 ULONG ref = InterlockedIncrement(&This->ref4);
373 TRACE("%p increasing refcount to %u.\n", This, ref);
375 if (ref == 1) InterlockedIncrement(&This->numIfaces);
380 static ULONG WINAPI ddraw3_AddRef(IDirectDraw3 *iface)
382 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
383 ULONG ref = InterlockedIncrement(&This->ref3);
385 TRACE("%p increasing refcount to %u.\n", This, ref);
387 if (ref == 1) InterlockedIncrement(&This->numIfaces);
392 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
394 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
395 ULONG ref = InterlockedIncrement(&This->ref2);
397 TRACE("%p increasing refcount to %u.\n", This, ref);
399 if (ref == 1) InterlockedIncrement(&This->numIfaces);
404 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
406 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
407 ULONG ref = InterlockedIncrement(&This->ref1);
409 TRACE("%p increasing refcount to %u.\n", This, ref);
411 if (ref == 1) InterlockedIncrement(&This->numIfaces);
416 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
418 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
420 TRACE("iface %p.\n", iface);
422 return ddraw7_AddRef(&This->IDirectDraw7_iface);
425 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
427 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
429 TRACE("iface %p.\n", iface);
431 return ddraw1_AddRef(&This->IDirectDraw_iface);
434 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
436 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
438 TRACE("iface %p.\n", iface);
440 return ddraw1_AddRef(&This->IDirectDraw_iface);
443 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
445 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
447 TRACE("iface %p.\n", iface);
449 return ddraw1_AddRef(&This->IDirectDraw_iface);
452 /*****************************************************************************
455 * Destroys a ddraw object if all refcounts are 0. This is to share code
456 * between the IDirectDrawX::Release functions
459 * This: DirectDraw object to destroy
461 *****************************************************************************/
462 static void ddraw_destroy(IDirectDrawImpl *This)
464 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
465 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
467 /* Destroy the device window if we created one */
468 if(This->devicewindow != 0)
470 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
471 DestroyWindow(This->devicewindow);
472 This->devicewindow = 0;
475 EnterCriticalSection(&ddraw_cs);
476 list_remove(&This->ddraw_list_entry);
477 LeaveCriticalSection(&ddraw_cs);
479 /* Release the attached WineD3D stuff */
480 wined3d_device_decref(This->wined3d_device);
481 wined3d_decref(This->wineD3D);
483 /* Now free the object */
484 HeapFree(GetProcessHeap(), 0, This);
487 /*****************************************************************************
488 * IDirectDraw7::Release
490 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
492 * Returns: The new refcount
493 *****************************************************************************/
494 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
496 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
497 ULONG ref = InterlockedDecrement(&This->ref7);
499 TRACE("%p decreasing refcount to %u.\n", This, ref);
501 if (!ref && !InterlockedDecrement(&This->numIfaces))
507 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
509 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
510 ULONG ref = InterlockedDecrement(&This->ref4);
512 TRACE("%p decreasing refcount to %u.\n", This, ref);
514 if (!ref && !InterlockedDecrement(&This->numIfaces))
520 static ULONG WINAPI ddraw3_Release(IDirectDraw3 *iface)
522 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
523 ULONG ref = InterlockedDecrement(&This->ref3);
525 TRACE("%p decreasing refcount to %u.\n", This, ref);
527 if (!ref && !InterlockedDecrement(&This->numIfaces))
533 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
535 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
536 ULONG ref = InterlockedDecrement(&This->ref2);
538 TRACE("%p decreasing refcount to %u.\n", This, ref);
540 if (!ref && !InterlockedDecrement(&This->numIfaces))
546 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
548 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
549 ULONG ref = InterlockedDecrement(&This->ref1);
551 TRACE("%p decreasing refcount to %u.\n", This, ref);
553 if (!ref && !InterlockedDecrement(&This->numIfaces))
559 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
561 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
563 TRACE("iface %p.\n", iface);
565 return ddraw7_Release(&This->IDirectDraw7_iface);
568 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
570 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
572 TRACE("iface %p.\n", iface);
574 return ddraw1_Release(&This->IDirectDraw_iface);
577 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
579 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
581 TRACE("iface %p.\n", iface);
583 return ddraw1_Release(&This->IDirectDraw_iface);
586 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
588 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
590 TRACE("iface %p.\n", iface);
592 return ddraw1_Release(&This->IDirectDraw_iface);
595 /*****************************************************************************
596 * IDirectDraw methods
597 *****************************************************************************/
599 /*****************************************************************************
600 * IDirectDraw7::SetCooperativeLevel
602 * Sets the cooperative level for the DirectDraw object, and the window
603 * assigned to it. The cooperative level determines the general behavior
604 * of the DirectDraw application
606 * Warning: This is quite tricky, as it's not really documented which
607 * cooperative levels can be combined with each other. If a game fails
608 * after this function, try to check the cooperative levels passed on
609 * Windows, and if it returns something different.
611 * If you think that this function caused the failure because it writes a
612 * fixme, be sure to run again with a +ddraw trace.
614 * What is known about cooperative levels (See the ddraw modes test):
615 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
616 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
617 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
618 * DDSCL_FULLSCREEN can be activated
619 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
621 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
622 * DDSCL_SETFOCUSWINDOW (partially),
623 * DDSCL_MULTITHREADED (work in progress)
625 * Unhandled flags, which should be implemented
626 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
627 * expect any difference to a normal window for wine)
628 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
629 * rendering (Possible test case: Half-Life)
631 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
633 * These don't seem very important for wine:
634 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
637 * DD_OK if the cooperative level was set successfully
638 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
639 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
640 * (Probably others too, have to investigate)
642 *****************************************************************************/
643 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
645 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
648 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
649 DDRAW_dump_cooperativelevel(cooplevel);
651 EnterCriticalSection(&ddraw_cs);
653 /* Get the old window */
654 window = This->dest_window;
656 /* Tests suggest that we need one of them: */
657 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
661 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
662 LeaveCriticalSection(&ddraw_cs);
663 return DDERR_INVALIDPARAMS;
666 /* Handle those levels first which set various hwnds */
667 if(cooplevel & DDSCL_SETFOCUSWINDOW)
669 /* This isn't compatible with a lot of flags */
670 if(cooplevel & ( DDSCL_MULTITHREADED |
671 DDSCL_CREATEDEVICEWINDOW |
676 DDSCL_SETDEVICEWINDOW |
681 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
682 LeaveCriticalSection(&ddraw_cs);
683 return DDERR_INVALIDPARAMS;
686 if( (This->cooperative_level & DDSCL_EXCLUSIVE) && window )
688 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
689 LeaveCriticalSection(&ddraw_cs);
690 return DDERR_HWNDALREADYSET;
693 This->focuswindow = hwnd;
694 /* Won't use the hwnd param for anything else */
697 /* Use the focus window for drawing too */
698 This->dest_window = This->focuswindow;
700 /* Destroy the device window, if we have one */
701 if(This->devicewindow)
703 DestroyWindow(This->devicewindow);
704 This->devicewindow = NULL;
707 LeaveCriticalSection(&ddraw_cs);
711 if(cooplevel & DDSCL_EXCLUSIVE)
713 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
715 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
716 LeaveCriticalSection(&ddraw_cs);
717 return DDERR_INVALIDPARAMS;
720 else if( !(cooplevel & DDSCL_NORMAL) )
722 TRACE("(%p) SetCooperativeLevel needs at least SetFocusWindow or Exclusive or Normal mode\n", This);
723 LeaveCriticalSection(&ddraw_cs);
724 return DDERR_INVALIDPARAMS;
727 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
728 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
729 wined3d_device_release_focus_window(This->wined3d_device);
731 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
733 if (This->cooperative_level & DDSCL_FULLSCREEN)
734 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
736 if (cooplevel & DDSCL_FULLSCREEN)
738 WINED3DDISPLAYMODE display_mode;
740 wined3d_get_adapter_display_mode(This->wineD3D, WINED3DADAPTER_DEFAULT, &display_mode);
741 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
742 display_mode.Width, display_mode.Height);
746 if ((cooplevel & DDSCL_EXCLUSIVE)
747 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
749 HRESULT hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
752 ERR("Failed to acquire focus window, hr %#x.\n", hr);
753 LeaveCriticalSection(&ddraw_cs);
758 /* Don't override focus windows or private device windows */
759 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
760 This->dest_window = hwnd;
762 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
764 /* Don't create a device window if a focus window is set */
765 if( !(This->focuswindow) )
767 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
768 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
769 NULL, NULL, NULL, NULL);
772 ERR("Failed to create window, last error %#x.\n", GetLastError());
773 LeaveCriticalSection(&ddraw_cs);
777 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
778 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
780 This->devicewindow = devicewindow;
781 This->dest_window = devicewindow;
785 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
786 wined3d_device_set_multithreaded(This->wined3d_device);
788 /* Unhandled flags */
789 if(cooplevel & DDSCL_ALLOWREBOOT)
790 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
791 if(cooplevel & DDSCL_ALLOWMODEX)
792 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
793 if(cooplevel & DDSCL_FPUSETUP)
794 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
796 /* Store the cooperative_level */
797 This->cooperative_level = cooplevel;
798 TRACE("SetCooperativeLevel retuning DD_OK\n");
799 LeaveCriticalSection(&ddraw_cs);
803 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
805 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
807 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
809 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
812 static HRESULT WINAPI ddraw3_SetCooperativeLevel(IDirectDraw3 *iface, HWND window, DWORD flags)
814 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
816 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
818 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
821 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
823 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
825 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
827 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
830 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
832 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
834 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
836 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
839 /*****************************************************************************
841 * Helper function for SetDisplayMode and RestoreDisplayMode
843 * Implements DirectDraw's SetDisplayMode, but ignores the value of
844 * ForceRefreshRate, since it is already handled by
845 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
846 * without worrying that ForceRefreshRate will override the refresh rate. For
847 * argument and return value documentation, see
848 * ddraw7_SetDisplayMode.
850 *****************************************************************************/
851 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
852 DWORD BPP, DWORD RefreshRate, DWORD Flags)
854 enum wined3d_format_id format;
855 WINED3DDISPLAYMODE Mode;
858 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
859 Height, BPP, RefreshRate, Flags);
861 EnterCriticalSection(&ddraw_cs);
862 if( !Width || !Height )
864 ERR("Width %u, Height %u, what to do?\n", Width, Height);
865 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
866 LeaveCriticalSection(&ddraw_cs);
872 case 8: format = WINED3DFMT_P8_UINT; break;
873 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
874 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
875 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
876 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
877 default: format = WINED3DFMT_UNKNOWN; break;
880 if (FAILED(hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode)))
882 ERR("Failed to get current display mode, hr %#x.\n", hr);
884 else if (Mode.Width == Width
885 && Mode.Height == Height
886 && Mode.Format == format
887 && Mode.RefreshRate == RefreshRate)
889 TRACE("Skipping redundant mode setting call.\n");
890 LeaveCriticalSection(&ddraw_cs);
894 /* Check the exclusive mode
895 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
896 return DDERR_NOEXCLUSIVEMODE;
897 * This is WRONG. Don't know if the SDK is completely
898 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
899 * is returned, but Half-Life 1.1.1.1 (Steam version)
904 Mode.Height = Height;
905 Mode.RefreshRate = RefreshRate;
906 Mode.Format = format;
908 /* TODO: The possible return values from msdn suggest that
909 * the screen mode can't be changed if a surface is locked
910 * or some drawing is in progress
913 /* TODO: Lose the primary surface */
914 hr = wined3d_device_set_display_mode(ddraw->wined3d_device, 0, &Mode);
916 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
918 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window);
919 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window, Width, Height);
922 LeaveCriticalSection(&ddraw_cs);
925 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
930 /*****************************************************************************
931 * IDirectDraw7::SetDisplayMode
933 * Sets the display screen resolution, color depth and refresh frequency
934 * when in fullscreen mode (in theory).
935 * Possible return values listed in the SDK suggest that this method fails
936 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
937 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
938 * It seems to be valid to pass 0 for With and Height, this has to be tested
939 * It could mean that the current video mode should be left as-is. (But why
943 * Height, Width: Screen dimension
944 * BPP: Color depth in Bits per pixel
945 * Refreshrate: Screen refresh rate
951 *****************************************************************************/
952 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
953 DWORD BPP, DWORD RefreshRate, DWORD Flags)
955 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
957 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
958 iface, Width, Height, BPP, RefreshRate, Flags);
960 if (force_refresh_rate != 0)
962 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
963 RefreshRate, force_refresh_rate);
964 RefreshRate = force_refresh_rate;
967 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
970 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
971 DWORD bpp, DWORD refresh_rate, DWORD flags)
973 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
975 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
976 iface, width, height, bpp, refresh_rate, flags);
978 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
981 static HRESULT WINAPI ddraw3_SetDisplayMode(IDirectDraw3 *iface, DWORD width, DWORD height,
982 DWORD bpp, DWORD refresh_rate, DWORD flags)
984 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
986 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
987 iface, width, height, bpp, refresh_rate, flags);
989 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
992 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
993 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
995 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
997 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
998 iface, width, height, bpp, refresh_rate, flags);
1000 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1003 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1005 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1007 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1009 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, 0, 0);
1012 /*****************************************************************************
1013 * IDirectDraw7::RestoreDisplayMode
1015 * Restores the display mode to what it was at creation time. Basically.
1017 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1018 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1019 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1020 * -> DD_1 is released. The screen should be left at 1024x768x32.
1021 * -> DD_2 is released. The screen should be set to 1400x1050x32
1022 * This case is unhandled right now, but Empire Earth does it this way.
1023 * (But perhaps there is something in SetCooperativeLevel to prevent this)
1025 * The msdn says that this method resets the display mode to what it was before
1026 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1030 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1032 *****************************************************************************/
1033 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1035 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1037 TRACE("iface %p.\n", iface);
1039 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
1042 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1044 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1046 TRACE("iface %p.\n", iface);
1048 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1051 static HRESULT WINAPI ddraw3_RestoreDisplayMode(IDirectDraw3 *iface)
1053 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1055 TRACE("iface %p.\n", iface);
1057 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1060 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1062 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1064 TRACE("iface %p.\n", iface);
1066 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1069 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1071 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1073 TRACE("iface %p.\n", iface);
1075 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1078 /*****************************************************************************
1079 * IDirectDraw7::GetCaps
1081 * Returns the drives capabilities
1083 * Used for version 1, 2, 4 and 7
1086 * DriverCaps: Structure to write the Hardware accelerated caps to
1087 * HelCaps: Structure to write the emulation caps to
1090 * This implementation returns DD_OK only
1092 *****************************************************************************/
1093 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1095 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1097 WINED3DCAPS winecaps;
1099 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1101 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1103 /* One structure must be != NULL */
1104 if( (!DriverCaps) && (!HELCaps) )
1106 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1107 return DDERR_INVALIDPARAMS;
1110 memset(&caps, 0, sizeof(caps));
1111 memset(&winecaps, 0, sizeof(winecaps));
1112 caps.dwSize = sizeof(caps);
1113 EnterCriticalSection(&ddraw_cs);
1114 hr = wined3d_device_get_device_caps(This->wined3d_device, &winecaps);
1117 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1118 LeaveCriticalSection(&ddraw_cs);
1122 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1123 LeaveCriticalSection(&ddraw_cs);
1125 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1129 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1130 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1131 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1132 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1133 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1134 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1135 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1136 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1137 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1138 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1139 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1140 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1141 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1142 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1143 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1145 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1148 if(DefaultSurfaceType == SURFACE_GDI) {
1149 caps.dwCaps &= ~DDCAPS_3D;
1150 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1152 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1153 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1154 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1159 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1160 if (TRACE_ON(ddraw))
1162 TRACE("Driver Caps :\n");
1163 DDRAW_dump_DDCAPS(DriverCaps);
1169 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1170 if (TRACE_ON(ddraw))
1172 TRACE("HEL Caps :\n");
1173 DDRAW_dump_DDCAPS(HELCaps);
1180 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1182 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1184 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1186 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1189 static HRESULT WINAPI ddraw3_GetCaps(IDirectDraw3 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1191 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1193 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1195 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1198 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1200 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1202 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1204 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1207 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1209 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1211 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1213 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1216 /*****************************************************************************
1217 * IDirectDraw7::Compact
1219 * No idea what it does, MSDN says it's not implemented.
1222 * DD_OK, but this is unchecked
1224 *****************************************************************************/
1225 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1227 TRACE("iface %p.\n", iface);
1232 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1234 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1236 TRACE("iface %p.\n", iface);
1238 return ddraw7_Compact(&This->IDirectDraw7_iface);
1241 static HRESULT WINAPI ddraw3_Compact(IDirectDraw3 *iface)
1243 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1245 TRACE("iface %p.\n", iface);
1247 return ddraw7_Compact(&This->IDirectDraw7_iface);
1250 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1252 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1254 TRACE("iface %p.\n", iface);
1256 return ddraw7_Compact(&This->IDirectDraw7_iface);
1259 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1261 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1263 TRACE("iface %p.\n", iface);
1265 return ddraw7_Compact(&This->IDirectDraw7_iface);
1268 /*****************************************************************************
1269 * IDirectDraw7::GetDisplayMode
1271 * Returns information about the current display mode
1273 * Exists in Version 1, 2, 4 and 7
1276 * DDSD: Address of a surface description structure to write the info to
1281 *****************************************************************************/
1282 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1284 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1286 WINED3DDISPLAYMODE Mode;
1289 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1291 EnterCriticalSection(&ddraw_cs);
1292 /* This seems sane */
1295 LeaveCriticalSection(&ddraw_cs);
1296 return DDERR_INVALIDPARAMS;
1299 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1300 * so one method can be used for all versions (Hopefully) */
1301 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1304 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1305 LeaveCriticalSection(&ddraw_cs);
1309 Size = DDSD->dwSize;
1310 memset(DDSD, 0, Size);
1312 DDSD->dwSize = Size;
1313 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1314 DDSD->dwWidth = Mode.Width;
1315 DDSD->dwHeight = Mode.Height;
1316 DDSD->u2.dwRefreshRate = 60;
1317 DDSD->ddsCaps.dwCaps = 0;
1318 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1319 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
1320 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1324 TRACE("Returning surface desc :\n");
1325 DDRAW_dump_surface_desc(DDSD);
1328 LeaveCriticalSection(&ddraw_cs);
1332 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1334 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1336 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1338 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, surface_desc);
1341 static HRESULT WINAPI ddraw3_GetDisplayMode(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc)
1343 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1345 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1347 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1350 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1352 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1354 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1356 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1359 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1361 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1363 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1365 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1368 /*****************************************************************************
1369 * IDirectDraw7::GetFourCCCodes
1371 * Returns an array of supported FourCC codes.
1373 * Exists in Version 1, 2, 4 and 7
1376 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1377 * of enumerated codes
1378 * Codes: Pointer to an array of DWORDs where the supported codes are written
1382 * Always returns DD_OK, as it's a stub for now
1384 *****************************************************************************/
1385 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1387 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1388 static const enum wined3d_format_id formats[] =
1390 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1391 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1392 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1394 DWORD count = 0, i, outsize;
1396 WINED3DDISPLAYMODE d3ddm;
1397 WINED3DSURFTYPE type = This->ImplType;
1399 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1401 wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
1403 outsize = NumCodes && Codes ? *NumCodes : 0;
1405 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
1407 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1409 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, WINED3DDEVTYPE_HAL,
1410 d3ddm.Format, 0, WINED3DRTYPE_SURFACE, formats[i], type);
1413 if (count < outsize)
1414 Codes[count] = formats[i];
1419 TRACE("Returning %u FourCC codes\n", count);
1426 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1428 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1430 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1432 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1435 static HRESULT WINAPI ddraw3_GetFourCCCodes(IDirectDraw3 *iface, DWORD *codes_count, DWORD *codes)
1437 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1439 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1441 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1444 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1446 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1448 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1450 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1453 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1455 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1457 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1459 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1462 /*****************************************************************************
1463 * IDirectDraw7::GetMonitorFrequency
1465 * Returns the monitor's frequency
1467 * Exists in Version 1, 2, 4 and 7
1470 * Freq: Pointer to a DWORD to write the frequency to
1473 * Always returns DD_OK
1475 *****************************************************************************/
1476 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1478 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1480 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1481 * but for now this should make the games happy
1487 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1489 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1491 TRACE("iface %p, frequency %p.\n", iface, frequency);
1493 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1496 static HRESULT WINAPI ddraw3_GetMonitorFrequency(IDirectDraw3 *iface, DWORD *frequency)
1498 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1500 TRACE("iface %p, frequency %p.\n", iface, frequency);
1502 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1505 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1507 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1509 TRACE("iface %p, frequency %p.\n", iface, frequency);
1511 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1514 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1516 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1518 TRACE("iface %p, frequency %p.\n", iface, frequency);
1520 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1523 /*****************************************************************************
1524 * IDirectDraw7::GetVerticalBlankStatus
1526 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1527 * too basically, but as it's a semi stub, I didn't create a function there
1530 * status: Pointer to a BOOL to be filled with the vertical blank status
1534 * DDERR_INVALIDPARAMS if status is NULL
1536 *****************************************************************************/
1537 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1539 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1541 TRACE("iface %p, status %p.\n", iface, status);
1543 /* This looks sane, the MSDN suggests it too */
1544 EnterCriticalSection(&ddraw_cs);
1547 LeaveCriticalSection(&ddraw_cs);
1548 return DDERR_INVALIDPARAMS;
1551 *status = This->fake_vblank;
1552 This->fake_vblank = !This->fake_vblank;
1553 LeaveCriticalSection(&ddraw_cs);
1557 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1559 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1561 TRACE("iface %p, status %p.\n", iface, status);
1563 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1566 static HRESULT WINAPI ddraw3_GetVerticalBlankStatus(IDirectDraw3 *iface, BOOL *status)
1568 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1570 TRACE("iface %p, status %p.\n", iface, status);
1572 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1575 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1577 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1579 TRACE("iface %p, status %p.\n", iface, status);
1581 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1584 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1586 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1588 TRACE("iface %p, status %p.\n", iface, status);
1590 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1593 /*****************************************************************************
1594 * IDirectDraw7::GetAvailableVidMem
1596 * Returns the total and free video memory
1599 * Caps: Specifies the memory type asked for
1600 * total: Pointer to a DWORD to be filled with the total memory
1601 * free: Pointer to a DWORD to be filled with the free memory
1605 * DDERR_INVALIDPARAMS of free and total are NULL
1607 *****************************************************************************/
1608 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1611 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1613 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1617 TRACE("(%p) Asked for memory with description: ", This);
1618 DDRAW_dump_DDSCAPS2(Caps);
1620 EnterCriticalSection(&ddraw_cs);
1622 /* Todo: System memory vs local video memory vs non-local video memory
1623 * The MSDN also mentions differences between texture memory and other
1624 * resources, but that's not important
1627 if( (!total) && (!free) )
1629 LeaveCriticalSection(&ddraw_cs);
1630 return DDERR_INVALIDPARAMS;
1634 *total = This->total_vidmem;
1636 *free = wined3d_device_get_available_texture_mem(This->wined3d_device);
1638 LeaveCriticalSection(&ddraw_cs);
1642 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1643 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1645 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1647 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1649 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, caps, total, free);
1652 static HRESULT WINAPI ddraw3_GetAvailableVidMem(IDirectDraw3 *iface, DDSCAPS *caps, DWORD *total,
1655 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1658 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1660 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1661 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1664 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1665 DDSCAPS *caps, DWORD *total, DWORD *free)
1667 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1670 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1672 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1673 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1676 /*****************************************************************************
1677 * IDirectDraw7::Initialize
1679 * Initializes a DirectDraw interface.
1682 * GUID: Interface identifier. Well, don't know what this is really good
1686 * Returns DD_OK on the first call,
1687 * DDERR_ALREADYINITIALIZED on repeated calls
1689 *****************************************************************************/
1690 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *Guid)
1692 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1694 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(Guid));
1696 if(This->initialized)
1698 return DDERR_ALREADYINITIALIZED;
1706 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1708 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1710 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1712 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1715 static HRESULT WINAPI ddraw3_Initialize(IDirectDraw3 *iface, GUID *guid)
1717 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1719 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1721 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1724 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1726 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1728 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1730 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1733 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1735 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1737 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1739 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1742 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1744 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1749 /*****************************************************************************
1750 * IDirectDraw7::FlipToGDISurface
1752 * "Makes the surface that the GDI writes to the primary surface"
1753 * Looks like some windows specific thing we don't have to care about.
1754 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1755 * show error boxes ;)
1756 * Well, just return DD_OK.
1759 * Always returns DD_OK
1761 *****************************************************************************/
1762 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1764 FIXME("iface %p stub!\n", iface);
1769 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1771 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1773 TRACE("iface %p.\n", iface);
1775 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1778 static HRESULT WINAPI ddraw3_FlipToGDISurface(IDirectDraw3 *iface)
1780 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1782 TRACE("iface %p.\n", iface);
1784 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1787 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1789 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1791 TRACE("iface %p.\n", iface);
1793 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1796 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1798 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1800 TRACE("iface %p.\n", iface);
1802 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1805 /*****************************************************************************
1806 * IDirectDraw7::WaitForVerticalBlank
1808 * This method allows applications to get in sync with the vertical blank
1810 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1811 * redraw the screen, most likely because of this stub
1814 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1815 * or DDWAITVB_BLOCKEND
1816 * h: Not used, according to MSDN
1819 * Always returns DD_OK
1821 *****************************************************************************/
1822 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1826 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1828 /* This function is called often, so print the fixme only once */
1831 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1835 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1836 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1837 return DDERR_UNSUPPORTED; /* unchecked */
1842 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1844 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1846 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1848 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1851 static HRESULT WINAPI ddraw3_WaitForVerticalBlank(IDirectDraw3 *iface, DWORD flags, HANDLE event)
1853 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1855 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1857 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1860 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1862 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1864 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1866 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1869 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1871 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1873 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1875 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1878 /*****************************************************************************
1879 * IDirectDraw7::GetScanLine
1881 * Returns the scan line that is being drawn on the monitor
1884 * Scanline: Address to write the scan line value to
1887 * Always returns DD_OK
1889 *****************************************************************************/
1890 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1892 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1893 static BOOL hide = FALSE;
1894 WINED3DDISPLAYMODE Mode;
1896 TRACE("iface %p, line %p.\n", iface, Scanline);
1898 /* This function is called often, so print the fixme only once */
1899 EnterCriticalSection(&ddraw_cs);
1902 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1906 wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1908 /* Fake the line sweeping of the monitor */
1909 /* FIXME: We should synchronize with a source to keep the refresh rate */
1910 *Scanline = This->cur_scanline++;
1911 /* Assume 20 scan lines in the vertical blank */
1912 if (This->cur_scanline >= Mode.Height + 20)
1913 This->cur_scanline = 0;
1915 LeaveCriticalSection(&ddraw_cs);
1919 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1921 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1923 TRACE("iface %p, line %p.\n", iface, line);
1925 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1928 static HRESULT WINAPI ddraw3_GetScanLine(IDirectDraw3 *iface, DWORD *line)
1930 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1932 TRACE("iface %p, line %p.\n", iface, line);
1934 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1937 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1939 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1941 TRACE("iface %p, line %p.\n", iface, line);
1943 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1946 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1948 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1950 TRACE("iface %p, line %p.\n", iface, line);
1952 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1955 /*****************************************************************************
1956 * IDirectDraw7::TestCooperativeLevel
1958 * Informs the application about the state of the video adapter, depending
1959 * on the cooperative level
1962 * DD_OK if the device is in a sane state
1963 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1964 * if the state is not correct(See below)
1966 *****************************************************************************/
1967 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1969 TRACE("iface %p.\n", iface);
1974 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1976 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1978 TRACE("iface %p.\n", iface);
1980 return ddraw7_TestCooperativeLevel(&This->IDirectDraw7_iface);
1983 /*****************************************************************************
1984 * IDirectDraw7::GetGDISurface
1986 * Returns the surface that GDI is treating as the primary surface.
1987 * For Wine this is the front buffer
1990 * GDISurface: Address to write the surface pointer to
1993 * DD_OK if the surface was found
1994 * DDERR_NOTFOUND if the GDI surface wasn't found
1996 *****************************************************************************/
1997 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1999 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2000 struct wined3d_surface *wined3d_surface;
2001 IDirectDrawSurface7 *ddsurf;
2005 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2007 /* Get the back buffer from the wineD3DDevice and search its
2008 * attached surfaces for the front buffer. */
2009 EnterCriticalSection(&ddraw_cs);
2010 hr = wined3d_device_get_back_buffer(This->wined3d_device,
2011 0, 0, WINED3DBACKBUFFER_TYPE_MONO, &wined3d_surface);
2012 if (FAILED(hr) || !wined3d_surface)
2014 ERR("IWineD3DDevice::GetBackBuffer failed\n");
2015 LeaveCriticalSection(&ddraw_cs);
2016 return DDERR_NOTFOUND;
2019 ddsurf = wined3d_surface_get_parent(wined3d_surface);
2020 wined3d_surface_decref(wined3d_surface);
2022 /* Find the front buffer */
2023 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
2024 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
2029 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
2032 /* The AddRef is OK this time */
2033 LeaveCriticalSection(&ddraw_cs);
2037 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2039 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2041 TRACE("iface %p, surface %p.\n", iface, surface);
2043 return ddraw7_GetGDISurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 **)surface);
2046 static HRESULT WINAPI ddraw3_GetGDISurface(IDirectDraw3 *iface, IDirectDrawSurface **surface)
2048 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2049 IDirectDrawSurface7 *surface7;
2052 TRACE("iface %p, surface %p.\n", iface, surface);
2054 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2055 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_iface : NULL;
2060 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2062 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2063 IDirectDrawSurface7 *surface7;
2066 TRACE("iface %p, surface %p.\n", iface, surface);
2068 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2069 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_iface : NULL;
2074 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2076 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2077 IDirectDrawSurface7 *surface7;
2080 TRACE("iface %p, surface %p.\n", iface, surface);
2082 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2083 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_iface : NULL;
2088 struct displaymodescallback_context
2090 LPDDENUMMODESCALLBACK func;
2094 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2096 struct displaymodescallback_context *cbcontext = context;
2099 memcpy(&desc, surface_desc, sizeof(desc));
2100 desc.dwSize = sizeof(desc);
2102 return cbcontext->func(&desc, cbcontext->context);
2105 /*****************************************************************************
2106 * IDirectDraw7::EnumDisplayModes
2108 * Enumerates the supported Display modes. The modes can be filtered with
2109 * the DDSD parameter.
2112 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2113 * versions (3 and older?) this is reserved and must be 0.
2114 * DDSD: Surface description to filter the modes
2115 * Context: Pointer passed back to the callback function
2116 * cb: Application-provided callback function
2120 * DDERR_INVALIDPARAMS if the callback wasn't set
2122 *****************************************************************************/
2123 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2124 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2126 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2127 unsigned int modenum, fmt;
2128 WINED3DDISPLAYMODE mode;
2129 DDSURFACEDESC2 callback_sd;
2130 WINED3DDISPLAYMODE *enum_modes = NULL;
2131 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2132 DDPIXELFORMAT pixelformat;
2134 static const enum wined3d_format_id checkFormatList[] =
2136 WINED3DFMT_B8G8R8X8_UNORM,
2137 WINED3DFMT_B5G6R5_UNORM,
2141 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2142 iface, Flags, DDSD, Context, cb);
2144 EnterCriticalSection(&ddraw_cs);
2145 /* This looks sane */
2148 LeaveCriticalSection(&ddraw_cs);
2149 return DDERR_INVALIDPARAMS;
2152 if(!(Flags & DDEDM_REFRESHRATES))
2154 enum_mode_array_size = 16;
2155 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2158 ERR("Out of memory\n");
2159 LeaveCriticalSection(&ddraw_cs);
2160 return DDERR_OUTOFMEMORY;
2164 pixelformat.dwSize = sizeof(pixelformat);
2165 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2168 while (wined3d_enum_adapter_modes(This->wineD3D, WINED3DADAPTER_DEFAULT,
2169 checkFormatList[fmt], modenum++, &mode) == WINED3D_OK)
2171 PixelFormat_WineD3DtoDD(&pixelformat, mode.Format);
2174 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
2175 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
2176 if(DDSD->dwFlags & DDSD_REFRESHRATE && mode.RefreshRate != DDSD->u2.dwRefreshRate) continue;
2177 if (DDSD->dwFlags & DDSD_PIXELFORMAT &&
2178 pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount) continue;
2181 if(!(Flags & DDEDM_REFRESHRATES))
2183 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2184 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2185 * to be reduced to a single unique result in such case.
2190 for (i = 0; i < enum_mode_count; i++)
2192 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
2193 enum_modes[i].Format == mode.Format)
2203 memset(&callback_sd, 0, sizeof(callback_sd));
2204 callback_sd.dwSize = sizeof(callback_sd);
2205 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2207 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2208 if(Flags & DDEDM_REFRESHRATES)
2210 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
2213 callback_sd.dwWidth = mode.Width;
2214 callback_sd.dwHeight = mode.Height;
2216 callback_sd.u4.ddpfPixelFormat=pixelformat;
2218 /* Calc pitch and DWORD align like MSDN says */
2219 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
2220 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2222 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2223 callback_sd.u2.dwRefreshRate);
2225 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2227 TRACE("Application asked to terminate the enumeration\n");
2228 HeapFree(GetProcessHeap(), 0, enum_modes);
2229 LeaveCriticalSection(&ddraw_cs);
2233 if(!(Flags & DDEDM_REFRESHRATES))
2235 if (enum_mode_count == enum_mode_array_size)
2237 WINED3DDISPLAYMODE *new_enum_modes;
2239 enum_mode_array_size *= 2;
2240 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2242 if (!new_enum_modes)
2244 ERR("Out of memory\n");
2245 HeapFree(GetProcessHeap(), 0, enum_modes);
2246 LeaveCriticalSection(&ddraw_cs);
2247 return DDERR_OUTOFMEMORY;
2250 enum_modes = new_enum_modes;
2253 enum_modes[enum_mode_count++] = mode;
2258 TRACE("End of enumeration\n");
2259 HeapFree(GetProcessHeap(), 0, enum_modes);
2260 LeaveCriticalSection(&ddraw_cs);
2264 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2265 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2267 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2269 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2270 iface, flags, surface_desc, context, callback);
2272 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, surface_desc, context, callback);
2275 static HRESULT WINAPI ddraw3_EnumDisplayModes(IDirectDraw3 *iface, DWORD flags,
2276 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2278 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2279 struct displaymodescallback_context cbcontext;
2281 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2282 iface, flags, surface_desc, context, callback);
2284 cbcontext.func = callback;
2285 cbcontext.context = context;
2287 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2288 &cbcontext, EnumDisplayModesCallbackThunk);
2291 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2292 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2294 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2295 struct displaymodescallback_context cbcontext;
2297 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2298 iface, flags, surface_desc, context, callback);
2300 cbcontext.func = callback;
2301 cbcontext.context = context;
2303 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2304 &cbcontext, EnumDisplayModesCallbackThunk);
2307 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2308 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2310 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2311 struct displaymodescallback_context cbcontext;
2313 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2314 iface, flags, surface_desc, context, callback);
2316 cbcontext.func = callback;
2317 cbcontext.context = context;
2319 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2320 &cbcontext, EnumDisplayModesCallbackThunk);
2323 /*****************************************************************************
2324 * IDirectDraw7::EvaluateMode
2326 * Used with IDirectDraw7::StartModeTest to test video modes.
2327 * EvaluateMode is used to pass or fail a mode, and continue with the next
2331 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2332 * Timeout: Returns the amount of seconds left before the mode would have
2333 * been failed automatically
2336 * This implementation always DD_OK, because it's a stub
2338 *****************************************************************************/
2339 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2341 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2343 /* When implementing this, implement it in WineD3D */
2348 /*****************************************************************************
2349 * IDirectDraw7::GetDeviceIdentifier
2351 * Returns the device identifier, which gives information about the driver
2352 * Our device identifier is defined at the beginning of this file.
2355 * DDDI: Address for the returned structure
2356 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2359 * On success it returns DD_OK
2360 * DDERR_INVALIDPARAMS if DDDI is NULL
2362 *****************************************************************************/
2363 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2364 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2366 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2369 return DDERR_INVALIDPARAMS;
2371 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2372 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2373 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2374 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2375 * bytes too long. So only copy the relevant part of the structure
2378 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2382 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2383 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2385 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2386 DDDEVICEIDENTIFIER2 identifier2;
2389 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2391 hr = ddraw7_GetDeviceIdentifier(&This->IDirectDraw7_iface, &identifier2, flags);
2392 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2397 /*****************************************************************************
2398 * IDirectDraw7::GetSurfaceFromDC
2400 * Returns the Surface for a GDI device context handle.
2401 * Is this related to IDirectDrawSurface::GetDC ???
2404 * hdc: hdc to return the surface for
2405 * Surface: Address to write the surface pointer to
2408 * Always returns DD_OK because it's a stub
2410 *****************************************************************************/
2411 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2412 IDirectDrawSurface7 **Surface)
2414 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2415 struct wined3d_surface *wined3d_surface;
2418 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2420 if (!Surface) return E_INVALIDARG;
2422 hr = wined3d_device_get_surface_from_dc(This->wined3d_device, hdc, &wined3d_surface);
2425 TRACE("No surface found for dc %p.\n", hdc);
2427 return DDERR_NOTFOUND;
2430 *Surface = wined3d_surface_get_parent(wined3d_surface);
2431 IDirectDrawSurface7_AddRef(*Surface);
2432 TRACE("Returning surface %p.\n", Surface);
2436 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2437 IDirectDrawSurface4 **surface)
2439 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2440 IDirectDrawSurface7 *surface7;
2443 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2445 if (!surface) return E_INVALIDARG;
2447 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2448 *surface = surface7 ? (IDirectDrawSurface4 *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_iface : NULL;
2453 static HRESULT WINAPI ddraw3_GetSurfaceFromDC(IDirectDraw3 *iface, HDC dc,
2454 IDirectDrawSurface **surface)
2456 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2458 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2460 return ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, (IDirectDrawSurface7 **)surface);
2463 /*****************************************************************************
2464 * IDirectDraw7::RestoreAllSurfaces
2466 * Calls the restore method of all surfaces
2471 * Always returns DD_OK because it's a stub
2473 *****************************************************************************/
2474 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2476 FIXME("iface %p stub!\n", iface);
2478 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2479 * get their parent and call their restore method. Do not implement
2480 * it in WineD3D, as restoring a surface means re-creating the
2486 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2488 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2490 TRACE("iface %p.\n", iface);
2492 return ddraw7_RestoreAllSurfaces(&This->IDirectDraw7_iface);
2495 /*****************************************************************************
2496 * IDirectDraw7::StartModeTest
2498 * Tests the specified video modes to update the system registry with
2499 * refresh rate information. StartModeTest starts the mode test,
2500 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2501 * isn't called within 15 seconds, the mode is failed automatically
2503 * As refresh rates are handled by the X server, I don't think this
2504 * Method is important
2507 * Modes: An array of mode specifications
2508 * NumModes: The number of modes in Modes
2509 * Flags: Some flags...
2512 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2513 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2516 *****************************************************************************/
2517 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2519 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2520 iface, Modes, NumModes, Flags);
2522 /* This looks sane */
2523 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2525 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2526 * As it is not, DDERR_TESTFINISHED is returned
2527 * (hopefully that's correct
2529 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2530 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2536 /*****************************************************************************
2537 * ddraw_recreate_surfaces_cb
2539 * Enumeration callback for ddraw_recreate_surface.
2540 * It re-recreates the WineD3DSurface. It's pretty straightforward
2542 *****************************************************************************/
2543 HRESULT WINAPI ddraw_recreate_surfaces_cb(IDirectDrawSurface7 *surf, DDSURFACEDESC2 *desc, void *Context)
2545 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
2546 struct wined3d_resource_desc wined3d_desc;
2547 struct wined3d_resource *wined3d_resource;
2548 IDirectDrawImpl *This = surfImpl->ddraw;
2549 struct wined3d_surface *wined3d_surface;
2550 struct wined3d_swapchain *swapchain;
2551 struct wined3d_clipper *clipper;
2555 TRACE("surface %p, surface_desc %p, context %p.\n",
2556 surf, desc, Context);
2558 /* For the enumeration */
2559 IDirectDrawSurface7_Release(surf);
2561 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
2563 /* Get the objects */
2564 swapchain = surfImpl->wined3d_swapchain;
2565 surfImpl->wined3d_swapchain = NULL;
2566 wined3d_surface = surfImpl->wined3d_surface;
2568 /* get the clipper */
2569 clipper = wined3d_surface_get_clipper(wined3d_surface);
2571 /* Get the surface properties */
2572 wined3d_resource = wined3d_surface_get_resource(wined3d_surface);
2573 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
2575 parent = wined3d_surface_get_parent(wined3d_surface);
2576 hr = wined3d_surface_create(This->wined3d_device, wined3d_desc.width, wined3d_desc.height,
2577 wined3d_desc.format, TRUE, FALSE, surfImpl->mipmap_level, wined3d_desc.usage, wined3d_desc.pool,
2578 wined3d_desc.multisample_type, wined3d_desc.multisample_quality, This->ImplType,
2579 parent, &ddraw_surface_wined3d_parent_ops, &surfImpl->wined3d_surface);
2582 surfImpl->wined3d_surface = wined3d_surface;
2586 wined3d_surface_set_clipper(surfImpl->wined3d_surface, clipper);
2588 /* TODO: Copy the surface content, except for render targets */
2590 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
2595 /* The backbuffers have the swapchain set as well, but the primary
2596 * owns it and destroys it. */
2597 if (surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2598 wined3d_device_uninit_gdi(This->wined3d_device);
2599 surfImpl->isRenderTarget = FALSE;
2603 if (!wined3d_surface_decref(wined3d_surface))
2604 TRACE("Surface released successful, next surface\n");
2606 ERR("Something's still holding the old WineD3DSurface\n");
2609 surfImpl->ImplType = This->ImplType;
2611 return DDENUMRET_OK;
2614 /*****************************************************************************
2615 * ddraw_recreate_surfaces
2617 * A function, that converts all wineD3DSurfaces to the new implementation type
2618 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
2619 * new WineD3DSurface, copies the content and releases the old surface
2621 *****************************************************************************/
2622 static HRESULT ddraw_recreate_surfaces(IDirectDrawImpl *This)
2624 DDSURFACEDESC2 desc;
2625 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
2627 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
2629 /* Should happen almost never */
2630 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
2632 wined3d_device_uninit_3d(This->wined3d_device);
2634 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
2636 memset(&desc, 0, sizeof(desc));
2637 desc.dwSize = sizeof(desc);
2639 return IDirectDraw7_EnumSurfaces(&This->IDirectDraw7_iface, 0, &desc, This,
2640 ddraw_recreate_surfaces_cb);
2643 /*****************************************************************************
2644 * ddraw_create_surface
2646 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2647 * with the passed parameters.
2650 * DDSD: Description of the surface to create
2651 * Surf: Address to store the interface pointer at
2656 *****************************************************************************/
2657 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2658 IDirectDrawSurfaceImpl **ppSurf, UINT level)
2660 WINED3DSURFTYPE ImplType = This->ImplType;
2663 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2664 This, pDDSD, ppSurf, level);
2666 if (TRACE_ON(ddraw))
2668 TRACE(" (%p) Requesting surface desc :\n", This);
2669 DDRAW_dump_surface_desc(pDDSD);
2672 /* Select the surface type, if it wasn't choosen yet */
2673 if(ImplType == SURFACE_UNKNOWN)
2675 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
2676 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2678 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
2679 ImplType = SURFACE_OPENGL;
2682 /* Otherwise use GDI surfaces for now */
2685 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
2686 ImplType = SURFACE_GDI;
2689 /* Policy if all surface implementations are available:
2690 * First, check if a default type was set with winecfg. If not,
2691 * try Xrender surfaces, and use them if they work. Next, check if
2692 * accelerated OpenGL is available, and use GL surfaces in this
2693 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
2694 * was created, always use OpenGL surfaces.
2696 * (Note: Xrender surfaces are not implemented for now, the
2697 * unaccelerated implementation uses GDI to render in Software)
2700 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
2701 * be re-created. This could be done with IDirectDrawSurface7::Restore
2703 This->ImplType = ImplType;
2707 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2708 && (This->ImplType != SURFACE_OPENGL)
2709 && DefaultSurfaceType == SURFACE_UNKNOWN)
2711 /* We have to change to OpenGL,
2712 * and re-create all WineD3DSurfaces
2714 ImplType = SURFACE_OPENGL;
2715 This->ImplType = ImplType;
2716 TRACE("(%p) Re-creating all surfaces\n", This);
2717 ddraw_recreate_surfaces(This);
2718 TRACE("(%p) Done recreating all surfaces\n", This);
2720 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2722 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2723 /* Do not fail surface creation, only fail 3D device creation */
2727 /* Create the Surface object */
2728 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2731 ERR("(%p) Error allocating memory for a surface\n", This);
2732 return DDERR_OUTOFVIDEOMEMORY;
2735 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, ImplType);
2738 WARN("Failed to initialize surface, hr %#x.\n", hr);
2739 HeapFree(GetProcessHeap(), 0, *ppSurf);
2743 /* Increase the surface counter, and attach the surface */
2744 InterlockedIncrement(&This->surfaces);
2745 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2747 TRACE("Created surface %p.\n", *ppSurf);
2751 /*****************************************************************************
2752 * CreateAdditionalSurfaces
2754 * Creates a new mipmap chain.
2757 * root: Root surface to attach the newly created chain to
2758 * count: number of surfaces to create
2759 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2760 * effects on the caller
2761 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2762 * creates an additional surface without the mipmapping flags
2764 *****************************************************************************/
2766 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2767 IDirectDrawSurfaceImpl *root,
2769 DDSURFACEDESC2 DDSD,
2772 UINT i, j, level = 0;
2774 IDirectDrawSurfaceImpl *last = root;
2776 for(i = 0; i < count; i++)
2778 IDirectDrawSurfaceImpl *object2 = NULL;
2780 /* increase the mipmap level, but only if a mipmap is created
2781 * In this case, also halve the size
2783 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2786 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2787 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2788 /* Set the mipmap sublevel flag according to msdn */
2789 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2793 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2795 CubeFaceRoot = FALSE;
2797 hr = ddraw_create_surface(This, &DDSD, &object2, level);
2803 /* Add the new surface to the complex attachment array */
2804 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2806 if(last->complex_array[j]) continue;
2807 last->complex_array[j] = object2;
2812 /* Remove the (possible) back buffer cap from the new surface description,
2813 * because only one surface in the flipping chain is a back buffer, one
2814 * is a front buffer, the others are just primary surfaces.
2816 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2821 /* Must set all attached surfaces (e.g. mipmaps) versions as well */
2822 static void ddraw_set_surface_version(IDirectDrawSurfaceImpl *surface, UINT version)
2826 TRACE("surface %p, version %u -> %u.\n", surface, surface->version, version);
2828 surface->version = version;
2829 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
2831 if (!surface->complex_array[i]) break;
2832 ddraw_set_surface_version(surface->complex_array[i], version);
2834 while ((surface = surface->next_attached))
2836 ddraw_set_surface_version(surface, version);
2840 /*****************************************************************************
2841 * ddraw_attach_d3d_device
2843 * Initializes the D3D capabilities of WineD3D
2846 * primary: The primary surface for D3D
2852 *****************************************************************************/
2853 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2855 WINED3DPRESENT_PARAMETERS localParameters;
2856 HWND window = ddraw->dest_window;
2859 TRACE("ddraw %p, primary %p.\n", ddraw, primary);
2861 if (!window || window == GetDesktopWindow())
2863 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2864 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2865 NULL, NULL, NULL, NULL);
2868 ERR("Failed to create window, last error %#x.\n", GetLastError());
2872 ShowWindow(window, SW_HIDE); /* Just to be sure */
2873 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2877 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2879 ddraw->d3d_window = window;
2881 /* Store the future Render Target surface */
2882 ddraw->d3d_target = primary;
2884 /* Use the surface description for the device parameters, not the device
2885 * settings. The application might render to an offscreen surface. */
2886 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2887 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2888 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2889 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2890 ? primary->surface_desc.dwBackBufferCount : 0;
2891 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2892 localParameters.MultiSampleQuality = 0;
2893 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2894 localParameters.hDeviceWindow = window;
2895 localParameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2896 localParameters.EnableAutoDepthStencil = TRUE;
2897 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2898 localParameters.Flags = 0;
2899 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2900 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2901 localParameters.AutoRestoreDisplayMode = FALSE;
2903 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2904 * recursive loop until ram or emulated video memory is full. */
2905 ddraw->d3d_initialized = TRUE;
2906 hr = wined3d_device_init_3d(ddraw->wined3d_device, &localParameters);
2909 ddraw->d3d_target = NULL;
2910 ddraw->d3d_initialized = FALSE;
2914 ddraw->declArraySize = 2;
2915 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2918 ERR("Error allocating an array for the converted vertex decls.\n");
2919 ddraw->declArraySize = 0;
2920 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
2921 return E_OUTOFMEMORY;
2924 TRACE("Successfully initialized 3D.\n");
2929 static HRESULT ddraw_create_gdi_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2931 WINED3DPRESENT_PARAMETERS presentation_parameters;
2935 window = ddraw->dest_window;
2937 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2939 /* Use the surface description for the device parameters, not the device
2940 * settings. The application might render to an offscreen surface. */
2941 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2942 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2943 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2944 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2945 ? primary->surface_desc.dwBackBufferCount : 0;
2946 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2947 presentation_parameters.MultiSampleQuality = 0;
2948 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2949 presentation_parameters.hDeviceWindow = window;
2950 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2951 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2952 presentation_parameters.AutoDepthStencilFormat = 0;
2953 presentation_parameters.Flags = 0;
2954 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2955 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2956 presentation_parameters.AutoRestoreDisplayMode = FALSE;
2958 ddraw->d3d_target = primary;
2959 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &presentation_parameters);
2960 ddraw->d3d_target = NULL;
2963 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
2964 primary->wined3d_swapchain = NULL;
2970 /*****************************************************************************
2971 * IDirectDraw7::CreateSurface
2973 * Creates a new IDirectDrawSurface object and returns its interface.
2975 * The surface connections with wined3d are a bit tricky. Basically it works
2978 * |------------------------| |-----------------|
2979 * | DDraw surface | | WineD3DSurface |
2981 * | WineD3DSurface |-------------->| |
2982 * | Child |<------------->| Parent |
2983 * |------------------------| |-----------------|
2985 * The DDraw surface is the parent of the wined3d surface, and it releases
2986 * the WineD3DSurface when the ddraw surface is destroyed.
2988 * However, for all surfaces which can be in a container in WineD3D,
2989 * we have to do this. These surfaces are usually complex surfaces,
2990 * so this concerns primary surfaces with a front and a back buffer,
2993 * |------------------------| |-----------------|
2994 * | DDraw surface | | Container |
2996 * | Child |<------------->| Parent |
2997 * | Texture |<------------->| |
2998 * | WineD3DSurface |<----| | Levels |<--|
2999 * | Complex connection | | | | |
3000 * |------------------------| | |-----------------| |
3004 * | |------------------| | |-----------------| |
3005 * | | IParent | |-------->| WineD3DSurface | |
3007 * | | Child |<------------->| Parent | |
3008 * | | | | Container |<--|
3009 * | |------------------| |-----------------| |
3011 * | |----------------------| |
3012 * | | DDraw surface 2 | |
3014 * |<->| Complex root Child | |
3016 * | | WineD3DSurface |<----| |
3017 * | |----------------------| | |
3019 * | |---------------------| | |-----------------| |
3020 * | | IParent | |----->| WineD3DSurface | |
3022 * | | Child |<---------->| Parent | |
3023 * | |---------------------| | Container |<--|
3024 * | |-----------------| |
3026 * | ---More surfaces can follow--- |
3028 * The reason is that the IWineD3DSwapchain(render target container)
3029 * and the IWineD3DTexure(Texture container) release the parents
3030 * of their surface's children, but by releasing the complex root
3031 * the surfaces which are complexly attached to it are destroyed
3032 * too. See IDirectDrawSurface::Release for a more detailed
3036 * DDSD: Description of the surface to create
3037 * Surf: Address to store the interface pointer at
3038 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
3039 * aggregation, so it has to be NULL
3043 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3044 * DDERR_* if an error occurs
3046 *****************************************************************************/
3047 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
3048 IDirectDrawSurface7 **Surf, IUnknown *UnkOuter)
3050 IDirectDrawSurfaceImpl *object = NULL;
3052 LONG extra_surfaces = 0;
3053 DDSURFACEDESC2 desc2;
3054 WINED3DDISPLAYMODE Mode;
3055 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
3057 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
3059 /* Some checks before we start */
3060 if (TRACE_ON(ddraw))
3062 TRACE(" (%p) Requesting surface desc :\n", ddraw);
3063 DDRAW_dump_surface_desc(DDSD);
3065 EnterCriticalSection(&ddraw_cs);
3067 if (UnkOuter != NULL)
3069 FIXME("(%p) : outer != NULL?\n", ddraw);
3070 LeaveCriticalSection(&ddraw_cs);
3071 return CLASS_E_NOAGGREGATION; /* unchecked */
3076 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
3077 LeaveCriticalSection(&ddraw_cs);
3078 return E_POINTER; /* unchecked */
3081 if (!(DDSD->dwFlags & DDSD_CAPS))
3083 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
3084 DDSD->dwFlags |= DDSD_CAPS;
3087 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
3089 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
3090 DDSD->dwFlags &= ~DDSD_LPSURFACE;
3093 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
3095 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
3096 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
3097 DDSD->dwFlags &= ~DDSD_LPSURFACE;
3100 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
3101 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
3103 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
3106 LeaveCriticalSection(&ddraw_cs);
3107 return DDERR_NOEXCLUSIVEMODE;
3110 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
3112 WARN("Application wanted to create back buffer primary surface\n");
3113 LeaveCriticalSection(&ddraw_cs);
3114 return DDERR_INVALIDCAPS;
3117 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
3119 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
3120 WARN("Application tries to put the surface in both system and video memory\n");
3121 LeaveCriticalSection(&ddraw_cs);
3123 return DDERR_INVALIDCAPS;
3126 /* Check cube maps but only if the size includes them */
3127 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
3129 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
3130 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
3132 WARN("Cube map faces requested without cube map flag\n");
3133 LeaveCriticalSection(&ddraw_cs);
3134 return DDERR_INVALIDCAPS;
3136 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
3137 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
3139 WARN("Cube map without faces requested\n");
3140 LeaveCriticalSection(&ddraw_cs);
3141 return DDERR_INVALIDPARAMS;
3144 /* Quick tests confirm those can be created, but we don't do that yet */
3145 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
3146 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
3148 FIXME("Partial cube maps not supported yet\n");
3152 /* According to the msdn this flag is ignored by CreateSurface */
3153 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
3154 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
3156 /* Modify some flags */
3157 copy_to_surfacedesc2(&desc2, DDSD);
3158 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
3160 /* Get the video mode from WineD3D - we will need it */
3161 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode);
3164 ERR("Failed to read display mode from wined3d\n");
3165 switch(ddraw->orig_bpp)
3168 Mode.Format = WINED3DFMT_P8_UINT;
3172 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
3176 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
3180 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
3184 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
3187 Mode.Width = ddraw->orig_width;
3188 Mode.Height = ddraw->orig_height;
3191 /* No pixelformat given? Use the current screen format */
3192 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
3194 desc2.dwFlags |= DDSD_PIXELFORMAT;
3195 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
3197 /* Wait: It could be a Z buffer */
3198 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3200 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
3203 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
3206 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
3209 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
3212 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
3215 ERR("Unknown Z buffer bit depth\n");
3220 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
3224 /* No Width or no Height? Use the original screen size
3226 if(!(desc2.dwFlags & DDSD_WIDTH) ||
3227 !(desc2.dwFlags & DDSD_HEIGHT) )
3229 /* Invalid for non-render targets */
3230 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3232 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
3234 LeaveCriticalSection(&ddraw_cs);
3235 return DDERR_INVALIDPARAMS;
3238 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
3239 desc2.dwWidth = Mode.Width;
3240 desc2.dwHeight = Mode.Height;
3243 if (!desc2.dwWidth || !desc2.dwHeight)
3245 LeaveCriticalSection(&ddraw_cs);
3246 return DDERR_INVALIDPARAMS;
3249 /* Mipmap count fixes */
3250 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3252 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
3254 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
3256 /* Mipmap count is given, should not be 0 */
3257 if( desc2.u2.dwMipMapCount == 0 )
3259 LeaveCriticalSection(&ddraw_cs);
3260 return DDERR_INVALIDPARAMS;
3265 /* Undocumented feature: Create sublevels until
3266 * either the width or the height is 1
3268 DWORD min = desc2.dwWidth < desc2.dwHeight ?
3269 desc2.dwWidth : desc2.dwHeight;
3270 desc2.u2.dwMipMapCount = 0;
3273 desc2.u2.dwMipMapCount += 1;
3280 /* Not-complex mipmap -> Mipmapcount = 1 */
3281 desc2.u2.dwMipMapCount = 1;
3283 extra_surfaces = desc2.u2.dwMipMapCount - 1;
3285 /* There's a mipmap count in the created surface in any case */
3286 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
3288 /* If no mipmap is given, the texture has only one level */
3290 /* The first surface is a front buffer, the back buffer is created afterwards */
3291 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
3293 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
3296 /* The root surface in a cube map is positive x */
3297 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3299 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3300 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3303 /* Create the first surface */
3304 hr = ddraw_create_surface(ddraw, &desc2, &object, 0);
3307 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
3308 LeaveCriticalSection(&ddraw_cs);
3311 object->is_complex_root = TRUE;
3313 *Surf = (IDirectDrawSurface7 *)object;
3315 /* Create Additional surfaces if necessary
3316 * This applies to Primary surfaces which have a back buffer count
3317 * set, but not to mipmap textures. In case of Mipmap textures,
3318 * wineD3D takes care of the creation of additional surfaces
3320 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
3322 extra_surfaces = DDSD->dwBackBufferCount;
3323 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
3324 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
3325 desc2.dwBackBufferCount = 0;
3329 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3331 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3332 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
3333 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3334 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
3335 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
3336 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3337 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
3338 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
3339 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3340 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
3341 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
3342 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3343 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
3344 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
3345 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3346 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
3347 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3350 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE);
3353 /* This destroys and possibly created surfaces too */
3354 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
3355 LeaveCriticalSection(&ddraw_cs);
3359 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
3360 * But attach the d3ddevice only if the currently created surface was
3361 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
3362 * The only case I can think of where this doesn't apply is when a
3363 * 2D app was configured by the user to run with OpenGL and it didn't create
3364 * the render target as first surface. In this case the render target creation
3365 * will cause the 3D init.
3367 if( (ddraw->ImplType == SURFACE_OPENGL) && !(ddraw->d3d_initialized) &&
3368 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
3370 IDirectDrawSurfaceImpl *target = object, *surface;
3373 /* Search for the primary to use as render target */
3374 LIST_FOR_EACH(entry, &ddraw->surface_list)
3376 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3377 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
3378 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
3382 TRACE("Using primary %p as render target\n", target);
3387 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", ddraw, target);
3388 hr = ddraw_attach_d3d_device(ddraw, target);
3391 IDirectDrawSurfaceImpl *release_surf;
3392 ERR("ddraw_attach_d3d_device failed, hr %#x\n", hr);
3395 /* The before created surface structures are in an incomplete state here.
3396 * WineD3D holds the reference on the IParents, and it released them on the failure
3397 * already. So the regular release method implementation would fail on the attempt
3398 * to destroy either the IParents or the swapchain. So free the surface here.
3399 * The surface structure here is a list, not a tree, because onscreen targets
3400 * cannot be cube textures
3404 release_surf = object;
3405 object = object->complex_array[0];
3406 ddraw_surface_destroy(release_surf);
3408 LeaveCriticalSection(&ddraw_cs);
3412 else if(!(ddraw->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3414 ddraw_create_gdi_swapchain(ddraw, object);
3417 /* Addref the ddraw interface to keep an reference for each surface */
3418 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
3419 object->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
3421 /* Create a WineD3DTexture if a texture was requested */
3422 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3424 ddraw->tex_root = object;
3425 ddraw_surface_create_texture(object);
3426 ddraw->tex_root = NULL;
3429 LeaveCriticalSection(&ddraw_cs);
3433 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
3434 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3436 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3438 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3439 iface, surface_desc, surface, outer_unknown);
3441 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3443 WARN("Application supplied invalid surface descriptor\n");
3444 return DDERR_INVALIDPARAMS;
3447 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3449 if (TRACE_ON(ddraw))
3451 TRACE(" (%p) Requesting surface desc :\n", iface);
3452 DDRAW_dump_surface_desc(surface_desc);
3455 WARN("Application tried to create an explicit front or back buffer\n");
3456 return DDERR_INVALIDCAPS;
3459 return CreateSurface(This, surface_desc, surface, outer_unknown);
3462 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3463 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3465 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3466 IDirectDrawSurfaceImpl *impl;
3469 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3470 iface, surface_desc, surface, outer_unknown);
3472 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3474 WARN("Application supplied invalid surface descriptor\n");
3475 return DDERR_INVALIDPARAMS;
3478 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3480 if (TRACE_ON(ddraw))
3482 TRACE(" (%p) Requesting surface desc :\n", iface);
3483 DDRAW_dump_surface_desc(surface_desc);
3486 WARN("Application tried to create an explicit front or back buffer\n");
3487 return DDERR_INVALIDCAPS;
3490 hr = CreateSurface(This, surface_desc, (IDirectDrawSurface7 **)surface, outer_unknown);
3491 impl = (IDirectDrawSurfaceImpl *)*surface;
3492 if (SUCCEEDED(hr) && impl)
3494 ddraw_set_surface_version(impl, 4);
3495 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3496 IDirectDraw4_AddRef(iface);
3497 impl->ifaceToRelease = (IUnknown *)iface;
3503 static HRESULT WINAPI ddraw3_CreateSurface(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc,
3504 IDirectDrawSurface **surface, IUnknown *outer_unknown)
3506 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3507 IDirectDrawSurface7 *surface7;
3508 IDirectDrawSurfaceImpl *impl;
3511 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3512 iface, surface_desc, surface, outer_unknown);
3514 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3516 WARN("Application supplied invalid surface descriptor\n");
3517 return DDERR_INVALIDPARAMS;
3520 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3522 if (TRACE_ON(ddraw))
3524 TRACE(" (%p) Requesting surface desc :\n", iface);
3525 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3528 WARN("Application tried to create an explicit front or back buffer\n");
3529 return DDERR_INVALIDCAPS;
3532 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3539 impl = (IDirectDrawSurfaceImpl *)surface7;
3540 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_iface;
3541 ddraw_set_surface_version(impl, 3);
3542 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3543 IDirectDraw3_AddRef(iface);
3544 impl->ifaceToRelease = (IUnknown *)iface;
3549 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3550 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3552 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3553 IDirectDrawSurface7 *surface7;
3554 IDirectDrawSurfaceImpl *impl;
3557 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3558 iface, surface_desc, surface, outer_unknown);
3560 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3562 WARN("Application supplied invalid surface descriptor\n");
3563 return DDERR_INVALIDPARAMS;
3566 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3568 if (TRACE_ON(ddraw))
3570 TRACE(" (%p) Requesting surface desc :\n", iface);
3571 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3574 WARN("Application tried to create an explicit front or back buffer\n");
3575 return DDERR_INVALIDCAPS;
3578 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3585 impl = (IDirectDrawSurfaceImpl *)surface7;
3586 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_iface;
3587 ddraw_set_surface_version(impl, 2);
3588 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3589 impl->ifaceToRelease = NULL;
3594 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3595 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3597 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3598 IDirectDrawSurface7 *surface7;
3599 IDirectDrawSurfaceImpl *impl;
3602 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3603 iface, surface_desc, surface, outer_unknown);
3605 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3607 WARN("Application supplied invalid surface descriptor\n");
3608 return DDERR_INVALIDPARAMS;
3611 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3612 * primaries anyway. */
3613 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3614 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3621 impl = (IDirectDrawSurfaceImpl *)surface7;
3622 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_iface;
3623 ddraw_set_surface_version(impl, 1);
3624 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3625 impl->ifaceToRelease = NULL;
3630 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3631 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3634 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3635 const DDPIXELFORMAT *provided)
3637 /* Some flags must be present in both or neither for a match. */
3638 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3639 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3640 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3642 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3645 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3648 if (requested->dwFlags & DDPF_FOURCC)
3649 if (requested->dwFourCC != provided->dwFourCC)
3652 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3653 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3654 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3657 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3658 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3659 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3662 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3663 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3666 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3667 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3669 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3672 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3673 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3679 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3688 #define CMP(FLAG, FIELD) \
3689 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3690 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3692 static const struct compare_info compare[] =
3694 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3695 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3697 CMP(CKDESTBLT, ddckCKDestBlt),
3698 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3699 CMP(CKSRCBLT, ddckCKSrcBlt),
3700 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3701 CMP(HEIGHT, dwHeight),
3702 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3703 CMP(LPSURFACE, lpSurface),
3704 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3705 CMP(PITCH, u1 /* lPitch */),
3706 /* PIXELFORMAT: manual */
3707 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3708 CMP(TEXTURESTAGE, dwTextureStage),
3709 CMP(WIDTH, dwWidth),
3710 /* ZBUFFERBITDEPTH: "obsolete" */
3717 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3720 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3722 if (requested->dwFlags & compare[i].flag
3723 && memcmp((const char *)provided + compare[i].offset,
3724 (const char *)requested + compare[i].offset,
3725 compare[i].size) != 0)
3729 if (requested->dwFlags & DDSD_PIXELFORMAT)
3731 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3732 &provided->u4.ddpfPixelFormat))
3739 #undef DDENUMSURFACES_SEARCHTYPE
3740 #undef DDENUMSURFACES_MATCHTYPE
3742 struct surfacescallback_context
3744 LPDDENUMSURFACESCALLBACK func;
3748 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3749 DDSURFACEDESC2 *surface_desc, void *context)
3751 struct surfacescallback_context *cbcontext = context;
3753 return cbcontext->func((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_iface,
3754 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3757 /*****************************************************************************
3758 * IDirectDraw7::EnumSurfaces
3760 * Loops through all surfaces attached to this device and calls the
3761 * application callback. This can't be relayed to WineD3DDevice,
3762 * because some WineD3DSurfaces' parents are IParent objects
3765 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3766 * DDSD: Description to filter for
3767 * Context: Application-provided pointer, it's passed unmodified to the
3769 * Callback: Address to call for each surface
3772 * DDERR_INVALIDPARAMS if the callback is NULL
3775 *****************************************************************************/
3776 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3777 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3779 /* The surface enumeration is handled by WineDDraw,
3780 * because it keeps track of all surfaces attached to
3781 * it. The filtering is done by our callback function,
3782 * because WineDDraw doesn't handle ddraw-like surface
3785 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3786 IDirectDrawSurfaceImpl *surf;
3788 DDSURFACEDESC2 desc;
3789 struct list *entry, *entry2;
3791 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3792 iface, Flags, DDSD, Context, Callback);
3794 all = Flags & DDENUMSURFACES_ALL;
3795 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3797 EnterCriticalSection(&ddraw_cs);
3801 LeaveCriticalSection(&ddraw_cs);
3802 return DDERR_INVALIDPARAMS;
3805 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3806 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3808 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3809 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3811 desc = surf->surface_desc;
3812 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
3813 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
3815 LeaveCriticalSection(&ddraw_cs);
3820 LeaveCriticalSection(&ddraw_cs);
3824 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3825 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3827 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3829 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3830 iface, flags, surface_desc, context, callback);
3832 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, surface_desc, context,
3833 (LPDDENUMSURFACESCALLBACK7)callback);
3836 static HRESULT WINAPI ddraw3_EnumSurfaces(IDirectDraw3 *iface, DWORD flags,
3837 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3839 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3840 struct surfacescallback_context cbcontext;
3842 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3843 iface, flags, surface_desc, context, callback);
3845 cbcontext.func = callback;
3846 cbcontext.context = context;
3848 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3849 &cbcontext, EnumSurfacesCallbackThunk);
3852 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3853 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3855 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3856 struct surfacescallback_context cbcontext;
3858 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3859 iface, flags, surface_desc, context, callback);
3861 cbcontext.func = callback;
3862 cbcontext.context = context;
3864 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3865 &cbcontext, EnumSurfacesCallbackThunk);
3868 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3869 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3871 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3872 struct surfacescallback_context cbcontext;
3874 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3875 iface, flags, surface_desc, context, callback);
3877 cbcontext.func = callback;
3878 cbcontext.context = context;
3880 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3881 &cbcontext, EnumSurfacesCallbackThunk);
3884 /*****************************************************************************
3885 * DirectDrawCreateClipper (DDRAW.@)
3887 * Creates a new IDirectDrawClipper object.
3890 * Clipper: Address to write the interface pointer to
3891 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3895 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3896 * E_OUTOFMEMORY if allocating the object failed
3898 *****************************************************************************/
3900 DirectDrawCreateClipper(DWORD Flags,
3901 LPDIRECTDRAWCLIPPER *Clipper,
3904 IDirectDrawClipperImpl* object;
3907 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3908 Flags, Clipper, UnkOuter);
3910 EnterCriticalSection(&ddraw_cs);
3911 if (UnkOuter != NULL)
3913 LeaveCriticalSection(&ddraw_cs);
3914 return CLASS_E_NOAGGREGATION;
3917 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3918 sizeof(IDirectDrawClipperImpl));
3921 LeaveCriticalSection(&ddraw_cs);
3922 return E_OUTOFMEMORY;
3925 hr = ddraw_clipper_init(object);
3928 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3929 HeapFree(GetProcessHeap(), 0, object);
3930 LeaveCriticalSection(&ddraw_cs);
3934 TRACE("Created clipper %p.\n", object);
3935 *Clipper = (IDirectDrawClipper *) object;
3936 LeaveCriticalSection(&ddraw_cs);
3940 /*****************************************************************************
3941 * IDirectDraw7::CreateClipper
3943 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3945 *****************************************************************************/
3946 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3947 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3949 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3950 iface, Flags, Clipper, UnkOuter);
3952 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3955 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3956 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3958 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3960 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3961 iface, flags, clipper, outer_unknown);
3963 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3966 static HRESULT WINAPI ddraw3_CreateClipper(IDirectDraw3 *iface, DWORD flags,
3967 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3969 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3971 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3972 iface, flags, clipper, outer_unknown);
3974 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3977 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3978 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3980 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3982 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3983 iface, flags, clipper, outer_unknown);
3985 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3988 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3989 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3991 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3993 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3994 iface, flags, clipper, outer_unknown);
3996 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3999 /*****************************************************************************
4000 * IDirectDraw7::CreatePalette
4002 * Creates a new IDirectDrawPalette object
4005 * Flags: The flags for the new clipper
4006 * ColorTable: Color table to assign to the new clipper
4007 * Palette: Address to write the interface pointer to
4008 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
4012 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
4013 * E_OUTOFMEMORY if allocating the object failed
4015 *****************************************************************************/
4016 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
4017 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
4019 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
4020 IDirectDrawPaletteImpl *object;
4023 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
4024 iface, Flags, ColorTable, Palette, pUnkOuter);
4026 EnterCriticalSection(&ddraw_cs);
4027 if(pUnkOuter != NULL)
4029 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
4030 LeaveCriticalSection(&ddraw_cs);
4031 return CLASS_E_NOAGGREGATION;
4034 /* The refcount test shows that a cooplevel is required for this */
4035 if(!This->cooperative_level)
4037 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
4038 LeaveCriticalSection(&ddraw_cs);
4039 return DDERR_NOCOOPERATIVELEVELSET;
4042 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
4045 ERR("Out of memory when allocating memory for a palette implementation\n");
4046 LeaveCriticalSection(&ddraw_cs);
4047 return E_OUTOFMEMORY;
4050 hr = ddraw_palette_init(object, This, Flags, ColorTable);
4053 WARN("Failed to initialize palette, hr %#x.\n", hr);
4054 HeapFree(GetProcessHeap(), 0, object);
4055 LeaveCriticalSection(&ddraw_cs);
4059 TRACE("Created palette %p.\n", object);
4060 *Palette = (IDirectDrawPalette *)object;
4061 LeaveCriticalSection(&ddraw_cs);
4065 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
4066 IDirectDrawPalette **palette, IUnknown *outer_unknown)
4068 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4071 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4072 iface, flags, entries, palette, outer_unknown);
4074 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4075 if (SUCCEEDED(hr) && *palette)
4077 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4078 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4079 IDirectDraw4_AddRef(iface);
4080 impl->ifaceToRelease = (IUnknown *)iface;
4085 static HRESULT WINAPI ddraw3_CreatePalette(IDirectDraw3 *iface, DWORD flags,
4086 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4088 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4091 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4092 iface, flags, entries, palette, outer_unknown);
4094 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4095 if (SUCCEEDED(hr) && *palette)
4097 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4098 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4099 IDirectDraw4_AddRef(iface);
4100 impl->ifaceToRelease = (IUnknown *)iface;
4106 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
4107 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4109 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4112 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4113 iface, flags, entries, palette, outer_unknown);
4115 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4116 if (SUCCEEDED(hr) && *palette)
4118 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4119 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4120 impl->ifaceToRelease = NULL;
4126 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
4127 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4129 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4132 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4133 iface, flags, entries, palette, outer_unknown);
4135 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4136 if (SUCCEEDED(hr) && *palette)
4138 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4139 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4140 impl->ifaceToRelease = NULL;
4146 /*****************************************************************************
4147 * IDirectDraw7::DuplicateSurface
4149 * Duplicates a surface. The surface memory points to the same memory as
4150 * the original surface, and it's released when the last surface referencing
4151 * it is released. I guess that's beyond Wine's surface management right now
4152 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
4153 * test application to implement this)
4156 * Src: Address of the source surface
4157 * Dest: Address to write the new surface pointer to
4160 * See IDirectDraw7::CreateSurface
4162 *****************************************************************************/
4163 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
4164 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
4166 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
4168 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
4170 /* For now, simply create a new, independent surface */
4171 return IDirectDraw7_CreateSurface(iface,
4172 &Surf->surface_desc,
4177 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
4178 IDirectDrawSurface4 **dst)
4180 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4182 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4184 return ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 *)src,
4185 (IDirectDrawSurface7 **)dst);
4188 static HRESULT WINAPI ddraw3_DuplicateSurface(IDirectDraw3 *iface, IDirectDrawSurface *src,
4189 IDirectDrawSurface **dst)
4191 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4192 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)src);
4193 IDirectDrawSurface7 *dst7;
4196 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4197 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 *)src_impl, &dst7);
4200 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_iface;
4204 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
4205 IDirectDrawSurface *src, IDirectDrawSurface **dst)
4207 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4208 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)src);
4209 IDirectDrawSurface7 *dst7;
4212 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4213 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 *)src_impl, &dst7);
4216 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_iface;
4220 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
4221 IDirectDrawSurface **dst)
4223 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4224 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)src);
4225 IDirectDrawSurface7 *dst7;
4228 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4229 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface, (IDirectDrawSurface7 *)src_impl, &dst7);
4232 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_iface;
4236 /*****************************************************************************
4237 * IDirect3D7::EnumDevices
4239 * The EnumDevices method for IDirect3D7. It enumerates all supported
4240 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
4243 * callback: Function to call for each enumerated device
4244 * context: Pointer to pass back to the app
4247 * D3D_OK, or the return value of the GetCaps call
4249 *****************************************************************************/
4250 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
4252 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4253 D3DDEVICEDESC7 device_desc7;
4254 D3DDEVICEDESC device_desc1;
4258 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4261 return DDERR_INVALIDPARAMS;
4263 EnterCriticalSection(&ddraw_cs);
4265 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
4268 LeaveCriticalSection(&ddraw_cs);
4272 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
4274 device_desc7.deviceGUID = *device_list7[i].device_guid;
4275 callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
4278 TRACE("End of enumeration.\n");
4280 LeaveCriticalSection(&ddraw_cs);
4285 /*****************************************************************************
4286 * IDirect3D3::EnumDevices
4288 * Enumerates all supported Direct3DDevice interfaces. This is the
4289 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
4291 * Version 1, 2 and 3
4294 * callback: Application-provided routine to call for each enumerated device
4295 * Context: Pointer to pass to the callback
4298 * D3D_OK on success,
4299 * The result of IDirect3DImpl_GetCaps if it failed
4301 *****************************************************************************/
4302 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4304 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
4306 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4307 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
4308 D3DDEVICEDESC7 device_desc7;
4311 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
4312 * name string. Let's put the string in a sufficiently sized array in
4313 * writable memory. */
4314 char device_name[50];
4315 strcpy(device_name,"Direct3D HEL");
4317 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4320 return DDERR_INVALIDPARAMS;
4322 EnterCriticalSection(&ddraw_cs);
4324 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
4327 LeaveCriticalSection(&ddraw_cs);
4331 /* Do I have to enumerate the reference id? Note from old d3d7:
4332 * "It seems that enumerating the reference IID on Direct3D 1 games
4333 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
4335 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
4336 * EnumReference which enables / disables enumerating the reference
4337 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
4338 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
4339 * demo directory suggest this.
4341 * Some games(GTA 2) seem to use the second enumerated device, so I have
4342 * to enumerate at least 2 devices. So enumerate the reference device to
4345 * Other games (Rollcage) tell emulation and hal device apart by certain
4346 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
4347 * limitation flag), and it refuses all devices that have the perspective
4348 * flag set. This way it refuses the emulation device, and HAL devices
4349 * never have POW2 unset in d3d7 on windows. */
4350 if (This->d3dversion != 1)
4352 static CHAR reference_description[] = "RGB Direct3D emulation";
4354 TRACE("Enumerating WineD3D D3DDevice interface.\n");
4355 hal_desc = device_desc1;
4356 hel_desc = device_desc1;
4357 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
4358 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4359 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4360 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4361 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4362 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
4363 device_name, &hal_desc, &hel_desc, context);
4364 if (hr != D3DENUMRET_OK)
4366 TRACE("Application cancelled the enumeration.\n");
4367 LeaveCriticalSection(&ddraw_cs);
4372 strcpy(device_name,"Direct3D HAL");
4374 TRACE("Enumerating HAL Direct3D device.\n");
4375 hal_desc = device_desc1;
4376 hel_desc = device_desc1;
4377 /* The hal device does not have the pow2 flag set in hel, but in hal. */
4378 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4379 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4380 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4381 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4382 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
4383 device_name, &hal_desc, &hel_desc, context);
4384 if (hr != D3DENUMRET_OK)
4386 TRACE("Application cancelled the enumeration.\n");
4387 LeaveCriticalSection(&ddraw_cs);
4391 TRACE("End of enumeration.\n");
4393 LeaveCriticalSection(&ddraw_cs);
4397 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4399 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4401 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4403 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4406 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4408 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4410 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4412 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4415 /*****************************************************************************
4416 * IDirect3D3::CreateLight
4418 * Creates an IDirect3DLight interface. This interface is used in
4419 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4420 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4421 * uses the IDirect3DDevice7 interface with D3D7 lights.
4423 * Version 1, 2 and 3
4426 * light: Address to store the new interface pointer
4427 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4432 * DDERR_OUTOFMEMORY if memory allocation failed
4433 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4435 *****************************************************************************/
4436 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
4437 IUnknown *outer_unknown)
4439 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4440 IDirect3DLightImpl *object;
4442 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4444 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4446 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4449 ERR("Failed to allocate light memory.\n");
4450 return DDERR_OUTOFMEMORY;
4453 d3d_light_init(object, This);
4455 TRACE("Created light %p.\n", object);
4456 *light = (IDirect3DLight *)object;
4461 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4463 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4465 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4467 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4470 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4472 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4474 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4476 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4479 /*****************************************************************************
4480 * IDirect3D3::CreateMaterial
4482 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4483 * and older versions. The IDirect3DMaterial implementation wraps its
4484 * functionality to IDirect3DDevice7::SetMaterial and friends.
4486 * Version 1, 2 and 3
4489 * material: Address to store the new interface's pointer to
4490 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4495 * DDERR_OUTOFMEMORY if memory allocation failed
4496 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4498 *****************************************************************************/
4499 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4500 IUnknown *outer_unknown)
4502 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4503 IDirect3DMaterialImpl *object;
4505 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4507 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4509 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4512 ERR("Failed to allocate material memory.\n");
4513 return DDERR_OUTOFMEMORY;
4516 d3d_material_init(object, This);
4518 TRACE("Created material %p.\n", object);
4519 *material = (IDirect3DMaterial3 *)object;
4524 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material, IUnknown *outer_unknown)
4526 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4527 IDirect3DMaterial3 *material3;
4530 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4532 hr = d3d3_CreateMaterial(&This->IDirect3D3_iface, &material3, outer_unknown);
4533 *material = material3 ? (IDirect3DMaterial2 *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial2_vtbl : NULL;
4535 TRACE("Returning material %p.\n", *material);
4540 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material, IUnknown *outer_unknown)
4542 IDirect3DMaterial3 *material3;
4545 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4547 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4549 hr = d3d3_CreateMaterial(&This->IDirect3D3_iface, &material3, outer_unknown);
4550 *material = material3 ? (IDirect3DMaterial *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial_vtbl : NULL;
4552 TRACE("Returning material %p.\n", *material);
4557 /*****************************************************************************
4558 * IDirect3D3::CreateViewport
4560 * Creates an IDirect3DViewport interface. This interface is used
4561 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4562 * it has been replaced by a viewport structure and
4563 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4564 * uses the IDirect3DDevice7 methods for its functionality
4567 * Viewport: Address to store the new interface pointer
4568 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4573 * DDERR_OUTOFMEMORY if memory allocation failed
4574 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4576 *****************************************************************************/
4577 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4578 IUnknown *outer_unknown)
4580 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4581 IDirect3DViewportImpl *object;
4583 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4585 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4587 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4590 ERR("Failed to allocate viewport memory.\n");
4591 return DDERR_OUTOFMEMORY;
4594 d3d_viewport_init(object, This);
4596 TRACE("Created viewport %p.\n", object);
4597 *viewport = (IDirect3DViewport3 *)object;
4602 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4604 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4606 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4608 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4612 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4614 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4616 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4618 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4622 /*****************************************************************************
4623 * IDirect3D3::FindDevice
4625 * This method finds a device with the requested properties and returns a
4626 * device description
4630 * fds: Describes the requested device characteristics
4631 * fdr: Returns the device description
4635 * DDERR_INVALIDPARAMS if no device was found
4637 *****************************************************************************/
4638 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4640 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4641 D3DDEVICEDESC7 desc7;
4642 D3DDEVICEDESC desc1;
4645 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4647 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4649 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4650 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4651 return DDERR_INVALIDPARAMS;
4653 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4654 && fds->dcmColorModel != D3DCOLOR_RGB)
4656 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4657 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4660 if (fds->dwFlags & D3DFDS_GUID)
4662 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4663 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4664 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4665 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4667 WARN("No match for this GUID.\n");
4668 return DDERR_NOTFOUND;
4673 hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc1, &desc7);
4674 if (hr != D3D_OK) return hr;
4676 /* Now return our own GUID */
4677 fdr->guid = IID_D3DDEVICE_WineD3D;
4678 fdr->ddHwDesc = desc1;
4679 fdr->ddSwDesc = desc1;
4681 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4686 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4688 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4690 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4692 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4695 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4697 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4699 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4701 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4704 /*****************************************************************************
4705 * IDirect3D7::CreateDevice
4707 * Creates an IDirect3DDevice7 interface.
4709 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4710 * DirectDraw surfaces and are created with
4711 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4712 * create the device object and QueryInterfaces for IDirect3DDevice
4715 * refiid: IID of the device to create
4716 * Surface: Initial rendertarget
4717 * Device: Address to return the interface pointer
4721 * DDERR_OUTOFMEMORY if memory allocation failed
4722 * DDERR_INVALIDPARAMS if a device exists already
4724 *****************************************************************************/
4725 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4726 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4728 IDirectDrawSurfaceImpl *target = (IDirectDrawSurfaceImpl *)surface;
4729 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4730 IDirect3DDeviceImpl *object;
4733 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4735 EnterCriticalSection(&ddraw_cs);
4738 /* Fail device creation if non-opengl surfaces are used. */
4739 if (This->ImplType != SURFACE_OPENGL)
4741 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4742 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4744 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4745 * is caught in CreateSurface or QueryInterface. */
4746 LeaveCriticalSection(&ddraw_cs);
4750 if (This->d3ddevice)
4752 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4753 LeaveCriticalSection(&ddraw_cs);
4754 return DDERR_INVALIDPARAMS;
4757 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4760 ERR("Failed to allocate device memory.\n");
4761 LeaveCriticalSection(&ddraw_cs);
4762 return DDERR_OUTOFMEMORY;
4765 hr = d3d_device_init(object, This, target);
4768 WARN("Failed to initialize device, hr %#x.\n", hr);
4769 HeapFree(GetProcessHeap(), 0, object);
4770 LeaveCriticalSection(&ddraw_cs);
4774 TRACE("Created device %p.\n", object);
4775 *device = (IDirect3DDevice7 *)object;
4777 LeaveCriticalSection(&ddraw_cs);
4781 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4782 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4784 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4787 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4788 iface, debugstr_guid(riid), surface, device, outer_unknown);
4790 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4792 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid, (IDirectDrawSurface7 *)surface,
4793 (IDirect3DDevice7 **)device);
4794 if (*device) *device = (IDirect3DDevice3 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice3_vtbl;
4799 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4800 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4802 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4803 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)surface);
4806 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4807 iface, debugstr_guid(riid), surface, device);
4809 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4810 surface_impl ? (IDirectDrawSurface7 *)surface_impl : NULL,
4811 (IDirect3DDevice7 **)device);
4812 if (*device) *device = (IDirect3DDevice2 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice2_vtbl;
4817 /*****************************************************************************
4818 * IDirect3D7::CreateVertexBuffer
4820 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4826 * desc: Requested Vertex buffer properties
4827 * vertex_buffer: Address to return the interface pointer at
4828 * flags: Some flags, should be 0
4832 * DDERR_OUTOFMEMORY if memory allocation failed
4833 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4834 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4836 *****************************************************************************/
4837 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4838 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4840 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4841 IDirect3DVertexBufferImpl *object;
4844 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4845 iface, desc, vertex_buffer, flags);
4847 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4849 TRACE("Vertex buffer description:\n");
4850 TRACE(" dwSize %u\n", desc->dwSize);
4851 TRACE(" dwCaps %#x\n", desc->dwCaps);
4852 TRACE(" FVF %#x\n", desc->dwFVF);
4853 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
4855 /* Now create the vertex buffer */
4856 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4859 ERR("Failed to allocate vertex buffer memory.\n");
4860 return DDERR_OUTOFMEMORY;
4863 hr = d3d_vertex_buffer_init(object, This, desc);
4866 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
4867 HeapFree(GetProcessHeap(), 0, object);
4871 TRACE("Created vertex buffer %p.\n", object);
4872 *vertex_buffer = (IDirect3DVertexBuffer7 *)object;
4877 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4878 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4880 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4883 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4884 iface, desc, vertex_buffer, flags, outer_unknown);
4886 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4888 hr = d3d7_CreateVertexBuffer(&This->IDirect3D7_iface, desc,
4889 (IDirect3DVertexBuffer7 **)vertex_buffer, flags);
4891 *vertex_buffer = (IDirect3DVertexBuffer *)&((IDirect3DVertexBufferImpl *)*vertex_buffer)->IDirect3DVertexBuffer_vtbl;
4896 /*****************************************************************************
4897 * IDirect3D7::EnumZBufferFormats
4899 * Enumerates all supported Z buffer pixel formats
4905 * callback: callback to call for each pixel format
4906 * context: Pointer to pass back to the callback
4910 * DDERR_INVALIDPARAMS if callback is NULL
4911 * For details, see IWineD3DDevice::EnumZBufferFormats
4913 *****************************************************************************/
4914 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4915 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4917 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4918 WINED3DDISPLAYMODE d3ddm;
4919 WINED3DDEVTYPE type;
4923 /* Order matters. Specifically, BattleZone II (full version) expects the
4924 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4925 static const enum wined3d_format_id formats[] =
4927 WINED3DFMT_S1_UINT_D15_UNORM,
4928 WINED3DFMT_D16_UNORM,
4929 WINED3DFMT_X8D24_UNORM,
4930 WINED3DFMT_S4X4_UINT_D24_UNORM,
4931 WINED3DFMT_D24_UNORM_S8_UINT,
4932 WINED3DFMT_D32_UNORM,
4935 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4936 iface, debugstr_guid(device_iid), callback, context);
4938 if (!callback) return DDERR_INVALIDPARAMS;
4940 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4941 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4942 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4944 TRACE("Asked for HAL device.\n");
4945 type = WINED3DDEVTYPE_HAL;
4947 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4948 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4950 TRACE("Asked for SW device.\n");
4951 type = WINED3DDEVTYPE_SW;
4953 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4955 TRACE("Asked for REF device.\n");
4956 type = WINED3DDEVTYPE_REF;
4958 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4960 TRACE("Asked for NULLREF device.\n");
4961 type = WINED3DDEVTYPE_NULLREF;
4965 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4966 type = WINED3DDEVTYPE_HAL;
4969 EnterCriticalSection(&ddraw_cs);
4970 /* We need an adapter format from somewhere to please wined3d and WGL.
4971 * Use the current display mode. So far all cards offer the same depth
4972 * stencil format for all modes, but if some do not and applications do
4973 * not like that we'll have to find some workaround, like iterating over
4974 * all imaginable formats and collecting all the depth stencil formats we
4976 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
4978 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4980 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
4981 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4984 DDPIXELFORMAT pformat;
4986 memset(&pformat, 0, sizeof(pformat));
4987 pformat.dwSize = sizeof(pformat);
4988 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4990 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4991 hr = callback(&pformat, context);
4992 if (hr != DDENUMRET_OK)
4994 TRACE("Format enumeration cancelled by application.\n");
4995 LeaveCriticalSection(&ddraw_cs);
5000 TRACE("End of enumeration.\n");
5002 LeaveCriticalSection(&ddraw_cs);
5006 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
5007 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
5009 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
5011 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
5012 iface, debugstr_guid(device_iid), callback, context);
5014 return d3d7_EnumZBufferFormats(&This->IDirect3D7_iface, device_iid, callback, context);
5017 /*****************************************************************************
5018 * IDirect3D7::EvictManagedTextures
5020 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
5021 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
5026 * D3D_OK, because it's a stub
5028 *****************************************************************************/
5029 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
5031 FIXME("iface %p stub!\n", iface);
5033 /* TODO: Just enumerate resources using IWineD3DDevice_EnumResources(),
5034 * then unload surfaces / textures. */
5039 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
5041 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
5043 TRACE("iface %p.\n", iface);
5045 return d3d7_EvictManagedTextures(&This->IDirect3D7_iface);
5048 /*****************************************************************************
5049 * IDirect3DImpl_GetCaps
5051 * This function retrieves the device caps from wined3d
5052 * and converts it into a D3D7 and D3D - D3D3 structure
5053 * This is a helper function called from various places in ddraw
5056 * wined3d: The interface to get the caps from
5057 * desc1: Old D3D <3 structure to fill (needed)
5058 * desc7: D3D7 device desc structure to fill (needed)
5061 * D3D_OK on success, or the return value of IWineD3D::GetCaps
5063 *****************************************************************************/
5064 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
5066 WINED3DCAPS wined3d_caps;
5069 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
5071 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
5073 EnterCriticalSection(&ddraw_cs);
5074 hr = wined3d_get_device_caps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
5075 LeaveCriticalSection(&ddraw_cs);
5078 WARN("Failed to get device caps, hr %#x.\n", hr);
5082 /* Copy the results into the d3d7 and d3d3 structures */
5083 desc7->dwDevCaps = wined3d_caps.DevCaps;
5084 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
5085 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
5086 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
5087 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
5088 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
5089 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
5090 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
5091 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
5092 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
5093 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
5095 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
5096 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
5098 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
5099 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
5100 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
5101 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
5103 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
5104 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
5105 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
5106 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
5108 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
5109 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
5111 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
5112 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
5114 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
5115 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
5117 /* Remove all non-d3d7 caps */
5118 desc7->dwDevCaps &= (
5119 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
5120 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
5121 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
5122 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
5123 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
5124 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
5125 D3DDEVCAPS_HWRASTERIZATION);
5127 desc7->dwStencilCaps &= (
5128 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
5129 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
5130 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
5134 desc7->dwTextureOpCaps &= (
5135 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
5136 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
5137 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
5138 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
5139 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
5140 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5141 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
5142 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
5144 desc7->dwVertexProcessingCaps &= (
5145 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
5146 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
5148 desc7->dpcLineCaps.dwMiscCaps &= (
5149 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
5150 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
5151 D3DPMISCCAPS_CULLCCW);
5153 desc7->dpcLineCaps.dwRasterCaps &= (
5154 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
5155 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
5156 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
5157 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
5158 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
5159 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
5160 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
5161 D3DPRASTERCAPS_ZFOG);
5163 desc7->dpcLineCaps.dwZCmpCaps &= (
5164 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5165 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5166 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5168 desc7->dpcLineCaps.dwSrcBlendCaps &= (
5169 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5170 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5171 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5172 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5173 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5175 desc7->dpcLineCaps.dwDestBlendCaps &= (
5176 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5177 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5178 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5179 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5180 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5182 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
5183 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5184 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5185 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5187 desc7->dpcLineCaps.dwShadeCaps &= (
5188 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
5189 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
5190 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
5191 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
5192 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
5193 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
5194 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
5196 desc7->dpcLineCaps.dwTextureCaps &= (
5197 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
5198 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
5199 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
5200 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
5202 desc7->dpcLineCaps.dwTextureFilterCaps &= (
5203 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
5204 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
5205 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
5206 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
5207 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
5208 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
5210 desc7->dpcLineCaps.dwTextureBlendCaps &= (
5211 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
5212 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
5213 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
5215 desc7->dpcLineCaps.dwTextureAddressCaps &= (
5216 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
5217 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
5219 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
5221 /* DirectX7 always has the np2 flag set, no matter what the card
5222 * supports. Some old games (Rollcage) check the caps incorrectly.
5223 * If wined3d supports nonpow2 textures it also has np2 conditional
5225 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
5228 /* Fill the missing members, and do some fixup */
5229 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
5230 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
5231 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
5232 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
5233 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
5234 desc7->dpcLineCaps.dwStippleWidth = 32;
5235 desc7->dpcLineCaps.dwStippleHeight = 32;
5236 /* Use the same for the TriCaps */
5237 desc7->dpcTriCaps = desc7->dpcLineCaps;
5239 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
5240 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
5241 desc7->dwMinTextureWidth = 1;
5242 desc7->dwMinTextureHeight = 1;
5244 /* Convert DWORDs safely to WORDs */
5245 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
5246 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
5247 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
5248 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
5250 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
5251 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
5252 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
5253 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
5255 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
5257 desc7->dwReserved1 = 0;
5258 desc7->dwReserved2 = 0;
5259 desc7->dwReserved3 = 0;
5260 desc7->dwReserved4 = 0;
5262 /* Fill the old structure */
5263 memset(desc1, 0, sizeof(*desc1));
5264 desc1->dwSize = sizeof(D3DDEVICEDESC);
5265 desc1->dwFlags = D3DDD_COLORMODEL
5267 | D3DDD_TRANSFORMCAPS
5269 | D3DDD_LIGHTINGCAPS
5272 | D3DDD_DEVICERENDERBITDEPTH
5273 | D3DDD_DEVICEZBUFFERBITDEPTH
5274 | D3DDD_MAXBUFFERSIZE
5275 | D3DDD_MAXVERTEXCOUNT;
5277 desc1->dcmColorModel = D3DCOLOR_RGB;
5278 desc1->dwDevCaps = desc7->dwDevCaps;
5279 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
5280 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
5281 desc1->bClipping = TRUE;
5282 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
5283 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
5284 | D3DLIGHTCAPS_PARALLELPOINT
5285 | D3DLIGHTCAPS_POINT
5286 | D3DLIGHTCAPS_SPOT;
5288 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
5289 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
5291 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
5292 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
5293 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
5294 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
5295 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
5296 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
5297 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
5298 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
5299 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
5300 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
5301 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
5302 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
5303 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
5305 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
5306 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
5307 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
5308 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
5309 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
5310 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
5311 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
5312 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
5313 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
5314 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
5315 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
5316 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
5317 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
5319 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
5320 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
5321 desc1->dwMaxBufferSize = 0;
5322 desc1->dwMaxVertexCount = 65536;
5323 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
5324 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
5325 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
5326 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
5327 desc1->dwMinStippleWidth = 1;
5328 desc1->dwMinStippleHeight = 1;
5329 desc1->dwMaxStippleWidth = 32;
5330 desc1->dwMaxStippleHeight = 32;
5331 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
5332 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
5333 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
5334 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
5335 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
5336 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
5337 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
5338 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
5339 desc1->dwStencilCaps = desc7->dwStencilCaps;
5340 desc1->dwFVFCaps = desc7->dwFVFCaps;
5341 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
5342 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
5343 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
5348 /*****************************************************************************
5349 * IDirectDraw7 VTable
5350 *****************************************************************************/
5351 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
5354 ddraw7_QueryInterface,
5359 ddraw7_CreateClipper,
5360 ddraw7_CreatePalette,
5361 ddraw7_CreateSurface,
5362 ddraw7_DuplicateSurface,
5363 ddraw7_EnumDisplayModes,
5364 ddraw7_EnumSurfaces,
5365 ddraw7_FlipToGDISurface,
5367 ddraw7_GetDisplayMode,
5368 ddraw7_GetFourCCCodes,
5369 ddraw7_GetGDISurface,
5370 ddraw7_GetMonitorFrequency,
5372 ddraw7_GetVerticalBlankStatus,
5374 ddraw7_RestoreDisplayMode,
5375 ddraw7_SetCooperativeLevel,
5376 ddraw7_SetDisplayMode,
5377 ddraw7_WaitForVerticalBlank,
5379 ddraw7_GetAvailableVidMem,
5381 ddraw7_GetSurfaceFromDC,
5383 ddraw7_RestoreAllSurfaces,
5384 ddraw7_TestCooperativeLevel,
5385 ddraw7_GetDeviceIdentifier,
5387 ddraw7_StartModeTest,
5391 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5394 ddraw4_QueryInterface,
5399 ddraw4_CreateClipper,
5400 ddraw4_CreatePalette,
5401 ddraw4_CreateSurface,
5402 ddraw4_DuplicateSurface,
5403 ddraw4_EnumDisplayModes,
5404 ddraw4_EnumSurfaces,
5405 ddraw4_FlipToGDISurface,
5407 ddraw4_GetDisplayMode,
5408 ddraw4_GetFourCCCodes,
5409 ddraw4_GetGDISurface,
5410 ddraw4_GetMonitorFrequency,
5412 ddraw4_GetVerticalBlankStatus,
5414 ddraw4_RestoreDisplayMode,
5415 ddraw4_SetCooperativeLevel,
5416 ddraw4_SetDisplayMode,
5417 ddraw4_WaitForVerticalBlank,
5419 ddraw4_GetAvailableVidMem,
5421 ddraw4_GetSurfaceFromDC,
5423 ddraw4_RestoreAllSurfaces,
5424 ddraw4_TestCooperativeLevel,
5425 ddraw4_GetDeviceIdentifier,
5428 static const struct IDirectDraw3Vtbl ddraw3_vtbl =
5431 ddraw3_QueryInterface,
5436 ddraw3_CreateClipper,
5437 ddraw3_CreatePalette,
5438 ddraw3_CreateSurface,
5439 ddraw3_DuplicateSurface,
5440 ddraw3_EnumDisplayModes,
5441 ddraw3_EnumSurfaces,
5442 ddraw3_FlipToGDISurface,
5444 ddraw3_GetDisplayMode,
5445 ddraw3_GetFourCCCodes,
5446 ddraw3_GetGDISurface,
5447 ddraw3_GetMonitorFrequency,
5449 ddraw3_GetVerticalBlankStatus,
5451 ddraw3_RestoreDisplayMode,
5452 ddraw3_SetCooperativeLevel,
5453 ddraw3_SetDisplayMode,
5454 ddraw3_WaitForVerticalBlank,
5456 ddraw3_GetAvailableVidMem,
5458 ddraw3_GetSurfaceFromDC,
5461 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5464 ddraw2_QueryInterface,
5469 ddraw2_CreateClipper,
5470 ddraw2_CreatePalette,
5471 ddraw2_CreateSurface,
5472 ddraw2_DuplicateSurface,
5473 ddraw2_EnumDisplayModes,
5474 ddraw2_EnumSurfaces,
5475 ddraw2_FlipToGDISurface,
5477 ddraw2_GetDisplayMode,
5478 ddraw2_GetFourCCCodes,
5479 ddraw2_GetGDISurface,
5480 ddraw2_GetMonitorFrequency,
5482 ddraw2_GetVerticalBlankStatus,
5484 ddraw2_RestoreDisplayMode,
5485 ddraw2_SetCooperativeLevel,
5486 ddraw2_SetDisplayMode,
5487 ddraw2_WaitForVerticalBlank,
5489 ddraw2_GetAvailableVidMem,
5492 static const struct IDirectDrawVtbl ddraw1_vtbl =
5495 ddraw1_QueryInterface,
5500 ddraw1_CreateClipper,
5501 ddraw1_CreatePalette,
5502 ddraw1_CreateSurface,
5503 ddraw1_DuplicateSurface,
5504 ddraw1_EnumDisplayModes,
5505 ddraw1_EnumSurfaces,
5506 ddraw1_FlipToGDISurface,
5508 ddraw1_GetDisplayMode,
5509 ddraw1_GetFourCCCodes,
5510 ddraw1_GetGDISurface,
5511 ddraw1_GetMonitorFrequency,
5513 ddraw1_GetVerticalBlankStatus,
5515 ddraw1_RestoreDisplayMode,
5516 ddraw1_SetCooperativeLevel,
5517 ddraw1_SetDisplayMode,
5518 ddraw1_WaitForVerticalBlank,
5521 static const struct IDirect3D7Vtbl d3d7_vtbl =
5523 /* IUnknown methods */
5524 d3d7_QueryInterface,
5527 /* IDirect3D7 methods */
5530 d3d7_CreateVertexBuffer,
5531 d3d7_EnumZBufferFormats,
5532 d3d7_EvictManagedTextures
5535 static const struct IDirect3D3Vtbl d3d3_vtbl =
5537 /* IUnknown methods */
5538 d3d3_QueryInterface,
5541 /* IDirect3D3 methods */
5544 d3d3_CreateMaterial,
5545 d3d3_CreateViewport,
5548 d3d3_CreateVertexBuffer,
5549 d3d3_EnumZBufferFormats,
5550 d3d3_EvictManagedTextures
5553 static const struct IDirect3D2Vtbl d3d2_vtbl =
5555 /* IUnknown methods */
5556 d3d2_QueryInterface,
5559 /* IDirect3D2 methods */
5562 d3d2_CreateMaterial,
5563 d3d2_CreateViewport,
5568 static const struct IDirect3DVtbl d3d1_vtbl =
5570 /* IUnknown methods */
5571 d3d1_QueryInterface,
5574 /* IDirect3D methods */
5578 d3d1_CreateMaterial,
5579 d3d1_CreateViewport,
5583 /*****************************************************************************
5586 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5587 * if none was found.
5589 * This function is in ddraw.c and the DDraw object space because D3D7
5590 * vertex buffers are created using the IDirect3D interface to the ddraw
5591 * object, so they can be valid across D3D devices(theoretically. The ddraw
5592 * object also owns the wined3d device
5596 * fvf: Fvf to find the decl for
5599 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5601 *****************************************************************************/
5602 struct wined3d_vertex_declaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5604 struct wined3d_vertex_declaration *pDecl = NULL;
5606 int p, low, high; /* deliberately signed */
5607 struct FvfToDecl *convertedDecls = This->decls;
5609 TRACE("Searching for declaration for fvf %08x... ", fvf);
5612 high = This->numConvertedDecls - 1;
5613 while(low <= high) {
5614 p = (low + high) >> 1;
5616 if(convertedDecls[p].fvf == fvf) {
5617 TRACE("found %p\n", convertedDecls[p].decl);
5618 return convertedDecls[p].decl;
5619 } else if(convertedDecls[p].fvf < fvf) {
5625 TRACE("not found. Creating and inserting at position %d.\n", low);
5627 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5628 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5629 if (hr != S_OK) return NULL;
5631 if(This->declArraySize == This->numConvertedDecls) {
5632 int grow = max(This->declArraySize / 2, 8);
5633 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5634 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5635 if (!convertedDecls)
5637 wined3d_vertex_declaration_decref(pDecl);
5640 This->decls = convertedDecls;
5641 This->declArraySize += grow;
5644 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5645 convertedDecls[low].decl = pDecl;
5646 convertedDecls[low].fvf = fvf;
5647 This->numConvertedDecls++;
5649 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5653 static inline struct IDirectDrawImpl *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5655 return CONTAINING_RECORD(device_parent, struct IDirectDrawImpl, device_parent);
5658 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5659 struct wined3d_device *device)
5661 TRACE("device_parent %p, device %p.\n", device_parent, device);
5664 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
5665 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5666 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
5668 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5669 IDirectDrawSurfaceImpl *surf = NULL;
5671 DDSCAPS2 searchcaps = ddraw->tex_root->surface_desc.ddsCaps;
5673 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5674 "\tpool %#x, level %u, face %u, surface %p.\n",
5675 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5677 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5680 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5681 TRACE("Asked for positive x\n");
5682 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5684 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5686 surf = ddraw->tex_root; break;
5687 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5688 TRACE("Asked for negative x\n");
5689 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5690 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5691 TRACE("Asked for positive y\n");
5692 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5693 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5694 TRACE("Asked for negative y\n");
5695 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5696 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5697 TRACE("Asked for positive z\n");
5698 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5699 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5700 TRACE("Asked for negative z\n");
5701 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5702 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5707 IDirectDrawSurface7 *attached;
5708 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)ddraw->tex_root, &searchcaps, &attached);
5709 surf = (IDirectDrawSurfaceImpl *)attached;
5710 IDirectDrawSurface7_Release(attached);
5712 if (!surf) ERR("root search surface not found\n");
5714 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5717 IDirectDrawSurface7 *attached;
5718 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
5719 if(!attached) ERR("Surface not found\n");
5720 surf = (IDirectDrawSurfaceImpl *)attached;
5721 IDirectDrawSurface7_Release(attached);
5725 /* Return the surface */
5726 *surface = surf->wined3d_surface;
5727 wined3d_surface_incref(*surface);
5729 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5734 static HRESULT WINAPI findRenderTarget(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *ctx)
5736 IDirectDrawSurfaceImpl *s = (IDirectDrawSurfaceImpl *)surface;
5737 IDirectDrawSurfaceImpl **target = ctx;
5739 if (!s->isRenderTarget)
5742 IDirectDrawSurface7_Release(surface);
5743 return DDENUMRET_CANCEL;
5746 /* Recurse into the surface tree */
5747 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
5749 IDirectDrawSurface7_Release(surface);
5750 if (*target) return DDENUMRET_CANCEL;
5752 return DDENUMRET_OK;
5755 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
5756 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
5757 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5758 struct wined3d_surface **surface)
5760 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5761 IDirectDrawSurfaceImpl *d3d_surface = ddraw->d3d_target;
5762 IDirectDrawSurfaceImpl *target = NULL;
5764 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5765 "\tmultisample_quality %u, lockable %u, surface %p.\n",
5766 device_parent, container_parent, width, height, format, multisample_type,
5767 multisample_quality, lockable, surface);
5769 if (d3d_surface->isRenderTarget)
5771 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
5775 target = d3d_surface;
5780 target = ddraw->d3d_target;
5781 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", ddraw);
5784 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
5786 *surface = target->wined3d_surface;
5787 wined3d_surface_incref(*surface);
5788 target->isRenderTarget = TRUE;
5790 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
5795 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
5796 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5797 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
5799 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5800 IDirectDrawSurfaceImpl *ddraw_surface;
5801 DDSURFACEDESC2 ddsd;
5804 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5805 "\tmultisample_quality %u, discard %u, surface %p.\n",
5806 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
5810 /* Create a DirectDraw surface */
5811 memset(&ddsd, 0, sizeof(ddsd));
5812 ddsd.dwSize = sizeof(ddsd);
5813 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
5814 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5815 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5816 ddsd.dwHeight = height;
5817 ddsd.dwWidth = width;
5820 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
5824 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
5827 ddraw->depthstencil = TRUE;
5828 hr = IDirectDraw7_CreateSurface(&ddraw->IDirectDraw7_iface, &ddsd,
5829 (IDirectDrawSurface7 **)&ddraw_surface, NULL);
5830 ddraw->depthstencil = FALSE;
5833 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
5837 *surface = ddraw_surface->wined3d_surface;
5838 wined3d_surface_incref(*surface);
5839 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
5844 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5845 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5846 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
5848 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5849 "format %#x, pool %#x, usage %#x, volume %p.\n",
5850 device_parent, container_parent, width, height, depth,
5851 format, pool, usage, volume);
5853 ERR("Not implemented!\n");
5858 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5859 WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
5861 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5862 IDirectDrawSurfaceImpl *iterator;
5865 TRACE("device_parent %p, present_parameters %p, swapchain %p.\n", device_parent, present_parameters, swapchain);
5867 hr = wined3d_swapchain_create(ddraw->wined3d_device, present_parameters,
5868 ddraw->ImplType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5871 WARN("Failed to create swapchain, hr %#x.\n", hr);
5876 ddraw->d3d_target->wined3d_swapchain = *swapchain;
5877 iterator = ddraw->d3d_target->complex_array[0];
5880 iterator->wined3d_swapchain = *swapchain;
5881 iterator = iterator->complex_array[0];
5887 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5889 device_parent_wined3d_device_created,
5890 device_parent_create_surface,
5891 device_parent_create_rendertarget,
5892 device_parent_create_depth_stencil,
5893 device_parent_create_volume,
5894 device_parent_create_swapchain,
5897 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5902 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5903 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5904 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5905 ddraw->IDirectDraw3_iface.lpVtbl = &ddraw3_vtbl;
5906 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5907 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5908 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5909 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5910 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5911 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5912 ddraw->numIfaces = 1;
5915 /* See comments in IDirectDrawImpl_CreateNewSurface for a description of
5917 ddraw->ImplType = DefaultSurfaceType;
5919 /* Get the current screen settings. */
5921 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5923 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5924 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5926 ddraw->wineD3D = wined3d_create(7, &ddraw->IDirectDraw7_iface);
5927 if (!ddraw->wineD3D)
5929 WARN("Failed to create a wined3d object.\n");
5930 return E_OUTOFMEMORY;
5933 hr = wined3d_device_create(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type,
5934 NULL, 0, &ddraw->device_parent, &ddraw->wined3d_device);
5937 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5938 wined3d_decref(ddraw->wineD3D);
5942 /* Get the amount of video memory */
5943 ddraw->total_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
5945 list_init(&ddraw->surface_list);