2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
35 { { 0x00010001, 0x00010001 } },
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
44 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
46 ddraw_null_wined3d_object_destroyed,
49 static inline IDirectDrawImpl *ddraw_from_ddraw1(IDirectDraw *iface)
51 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw_vtbl));
54 static inline IDirectDrawImpl *ddraw_from_ddraw2(IDirectDraw2 *iface)
56 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw2_vtbl));
59 static inline IDirectDrawImpl *ddraw_from_ddraw3(IDirectDraw3 *iface)
61 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw3_vtbl));
64 static inline IDirectDrawImpl *ddraw_from_ddraw4(IDirectDraw4 *iface)
66 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw4_vtbl));
69 /*****************************************************************************
71 *****************************************************************************/
73 /*****************************************************************************
74 * IDirectDraw7::QueryInterface
76 * Queries different interfaces of the DirectDraw object. It can return
77 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
78 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
80 * The returned interface is AddRef()-ed before it's returned
82 * Used for version 1, 2, 4 and 7
85 * refiid: Interface ID asked for
86 * obj: Used to return the interface pointer
89 * S_OK if an interface was found
90 * E_NOINTERFACE if the requested interface wasn't found
92 *****************************************************************************/
93 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
95 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
97 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
99 /* Can change surface impl type */
100 EnterCriticalSection(&ddraw_cs);
102 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
107 LeaveCriticalSection(&ddraw_cs);
108 return DDERR_INVALIDPARAMS;
111 /* Check DirectDraw Interfaces */
112 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
113 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
116 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
118 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
120 *obj = &This->IDirectDraw4_vtbl;
121 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
123 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
125 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
126 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
128 LeaveCriticalSection(&ddraw_cs);
129 return E_NOINTERFACE;
131 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
133 *obj = &This->IDirectDraw2_vtbl;
134 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
136 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
138 *obj = &This->IDirectDraw_vtbl;
139 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
143 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
144 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
145 * who had this idea and why. The older interfaces can query and IDirect3D version
146 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
147 * and messy to implement with the common creation function, so it has been left out here.
149 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
150 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
151 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
152 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
154 /* Check the surface implementation */
155 if(This->ImplType == SURFACE_UNKNOWN)
157 /* Apps may create the IDirect3D Interface before the primary surface.
158 * set the surface implementation */
159 This->ImplType = SURFACE_OPENGL;
160 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
162 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
164 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
165 ERR(" (%p) You may want to contact wine-devel for help\n", This);
166 /* Should I assert(0) here??? */
168 else if(This->ImplType != SURFACE_OPENGL)
170 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
171 /* Do not abort here, only reject 3D Device creation */
174 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
176 This->d3dversion = 1;
177 *obj = &This->IDirect3D_vtbl;
178 TRACE(" returning Direct3D interface at %p.\n", *obj);
180 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
182 This->d3dversion = 2;
183 *obj = &This->IDirect3D2_vtbl;
184 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
186 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
188 This->d3dversion = 3;
189 *obj = &This->IDirect3D3_vtbl;
190 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
192 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
194 This->d3dversion = 7;
195 *obj = &This->IDirect3D7_vtbl;
196 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
199 else if (IsEqualGUID(refiid, &IID_IWineD3DDeviceParent))
201 *obj = &This->device_parent_vtbl;
204 /* Unknown interface */
207 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
208 LeaveCriticalSection(&ddraw_cs);
209 return E_NOINTERFACE;
212 IUnknown_AddRef( (IUnknown *) *obj );
213 LeaveCriticalSection(&ddraw_cs);
217 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
219 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
221 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw4(iface), riid, object);
224 static HRESULT WINAPI ddraw3_QueryInterface(IDirectDraw3 *iface, REFIID riid, void **object)
226 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
228 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw3(iface), riid, object);
231 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
233 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
235 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw2(iface), riid, object);
238 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
240 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
242 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw1(iface), riid, object);
245 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
247 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
249 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_d3d7(iface), riid, object);
252 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
254 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
256 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_d3d3(iface), riid, object);
259 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
261 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
263 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_d3d2(iface), riid, object);
266 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
268 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
270 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_d3d1(iface), riid, object);
273 /*****************************************************************************
274 * IDirectDraw7::AddRef
276 * Increases the interfaces refcount, basically
278 * DDraw refcounting is a bit tricky. The different DirectDraw interface
279 * versions have individual refcounts, but the IDirect3D interfaces do not.
280 * All interfaces are from one object, that means calling QueryInterface on an
281 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
282 * IDirectDrawImpl object.
284 * That means all AddRef and Release implementations of IDirectDrawX work
285 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
286 * except of IDirect3D7 which thunks to IDirectDraw7
288 * Returns: The new refcount
290 *****************************************************************************/
291 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
293 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
294 ULONG ref = InterlockedIncrement(&This->ref7);
296 TRACE("%p increasing refcount to %u.\n", This, ref);
298 if(ref == 1) InterlockedIncrement(&This->numIfaces);
303 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
305 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
306 ULONG ref = InterlockedIncrement(&ddraw->ref4);
308 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
310 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
315 static ULONG WINAPI ddraw3_AddRef(IDirectDraw3 *iface)
317 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
318 ULONG ref = InterlockedIncrement(&ddraw->ref3);
320 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
322 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
327 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
329 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
330 ULONG ref = InterlockedIncrement(&ddraw->ref2);
332 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
334 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
339 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
341 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
342 ULONG ref = InterlockedIncrement(&ddraw->ref1);
344 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
346 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
351 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
353 TRACE("iface %p.\n", iface);
355 return ddraw7_AddRef((IDirectDraw7 *)ddraw_from_d3d7(iface));
358 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
360 TRACE("iface %p.\n", iface);
362 return ddraw1_AddRef((IDirectDraw *)&ddraw_from_d3d3(iface)->IDirectDraw_vtbl);
365 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
367 TRACE("iface %p.\n", iface);
369 return ddraw1_AddRef((IDirectDraw *)&ddraw_from_d3d2(iface)->IDirectDraw_vtbl);
372 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
374 TRACE("iface %p.\n", iface);
376 return ddraw1_AddRef((IDirectDraw *)&ddraw_from_d3d1(iface)->IDirectDraw_vtbl);
379 /*****************************************************************************
382 * Destroys a ddraw object if all refcounts are 0. This is to share code
383 * between the IDirectDrawX::Release functions
386 * This: DirectDraw object to destroy
388 *****************************************************************************/
389 static void ddraw_destroy(IDirectDrawImpl *This)
391 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
392 IDirectDraw7_RestoreDisplayMode((IDirectDraw7 *)This);
394 /* Destroy the device window if we created one */
395 if(This->devicewindow != 0)
397 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
398 DestroyWindow(This->devicewindow);
399 This->devicewindow = 0;
402 EnterCriticalSection(&ddraw_cs);
403 list_remove(&This->ddraw_list_entry);
404 LeaveCriticalSection(&ddraw_cs);
406 /* Release the attached WineD3D stuff */
407 IWineD3DDevice_Release(This->wineD3DDevice);
408 IWineD3D_Release(This->wineD3D);
410 /* Now free the object */
411 HeapFree(GetProcessHeap(), 0, This);
414 /*****************************************************************************
415 * IDirectDraw7::Release
417 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
419 * Returns: The new refcount
420 *****************************************************************************/
421 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
423 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
424 ULONG ref = InterlockedDecrement(&This->ref7);
426 TRACE("%p decreasing refcount to %u.\n", This, ref);
428 if (!ref && !InterlockedDecrement(&This->numIfaces))
434 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
436 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
437 ULONG ref = InterlockedDecrement(&ddraw->ref4);
439 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
441 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
442 ddraw_destroy(ddraw);
447 static ULONG WINAPI ddraw3_Release(IDirectDraw3 *iface)
449 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
450 ULONG ref = InterlockedDecrement(&ddraw->ref3);
452 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
454 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
455 ddraw_destroy(ddraw);
460 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
462 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
463 ULONG ref = InterlockedDecrement(&ddraw->ref2);
465 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
467 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
468 ddraw_destroy(ddraw);
473 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
475 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
476 ULONG ref = InterlockedDecrement(&ddraw->ref1);
478 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
480 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
481 ddraw_destroy(ddraw);
486 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
488 TRACE("iface %p.\n", iface);
490 return ddraw7_Release((IDirectDraw7 *)ddraw_from_d3d7(iface));
493 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
495 TRACE("iface %p.\n", iface);
497 return ddraw1_Release((IDirectDraw *)&ddraw_from_d3d3(iface)->IDirectDraw_vtbl);
500 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
502 TRACE("iface %p.\n", iface);
504 return ddraw1_Release((IDirectDraw *)&ddraw_from_d3d2(iface)->IDirectDraw_vtbl);
507 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
509 TRACE("iface %p.\n", iface);
511 return ddraw1_Release((IDirectDraw *)&ddraw_from_d3d1(iface)->IDirectDraw_vtbl);
514 /*****************************************************************************
515 * IDirectDraw methods
516 *****************************************************************************/
518 /*****************************************************************************
519 * IDirectDraw7::SetCooperativeLevel
521 * Sets the cooperative level for the DirectDraw object, and the window
522 * assigned to it. The cooperative level determines the general behavior
523 * of the DirectDraw application
525 * Warning: This is quite tricky, as it's not really documented which
526 * cooperative levels can be combined with each other. If a game fails
527 * after this function, try to check the cooperative levels passed on
528 * Windows, and if it returns something different.
530 * If you think that this function caused the failure because it writes a
531 * fixme, be sure to run again with a +ddraw trace.
533 * What is known about cooperative levels (See the ddraw modes test):
534 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
535 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
536 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
537 * DDSCL_FULLSCREEN can be activated
538 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
540 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
541 * DDSCL_SETFOCUSWINDOW (partially),
542 * DDSCL_MULTITHREADED (work in progress)
544 * Unhandled flags, which should be implemented
545 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
546 * expect any difference to a normal window for wine)
547 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
548 * rendering (Possible test case: Half-Life)
550 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
552 * These don't seem very important for wine:
553 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
556 * DD_OK if the cooperative level was set successfully
557 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
558 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
559 * (Probably others too, have to investigate)
561 *****************************************************************************/
562 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
564 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
567 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
568 DDRAW_dump_cooperativelevel(cooplevel);
570 EnterCriticalSection(&ddraw_cs);
572 /* Get the old window */
573 window = This->dest_window;
575 /* Tests suggest that we need one of them: */
576 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
580 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
581 LeaveCriticalSection(&ddraw_cs);
582 return DDERR_INVALIDPARAMS;
585 /* Handle those levels first which set various hwnds */
586 if(cooplevel & DDSCL_SETFOCUSWINDOW)
588 /* This isn't compatible with a lot of flags */
589 if(cooplevel & ( DDSCL_MULTITHREADED |
594 DDSCL_SETDEVICEWINDOW |
599 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
600 LeaveCriticalSection(&ddraw_cs);
601 return DDERR_INVALIDPARAMS;
603 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
605 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
606 LeaveCriticalSection(&ddraw_cs);
607 return DDERR_HWNDALREADYSET;
610 This->focuswindow = hwnd;
611 /* Won't use the hwnd param for anything else */
614 /* Use the focus window for drawing too */
615 This->dest_window = This->focuswindow;
617 /* Destroy the device window, if we have one */
618 if(This->devicewindow)
620 DestroyWindow(This->devicewindow);
621 This->devicewindow = NULL;
624 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
625 if(cooplevel & DDSCL_NORMAL)
627 /* Can't coexist with fullscreen or exclusive */
628 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
630 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
631 LeaveCriticalSection(&ddraw_cs);
632 return DDERR_INVALIDPARAMS;
635 /* Switching from fullscreen? */
636 if(This->cooperative_level & DDSCL_FULLSCREEN)
638 This->cooperative_level &= ~DDSCL_FULLSCREEN;
639 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
640 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
642 IWineD3DDevice_ReleaseFocusWindow(This->wineD3DDevice);
645 /* Don't override focus windows or private device windows */
647 !(This->focuswindow) &&
648 !(This->devicewindow) &&
651 This->dest_window = hwnd;
654 else if(cooplevel & DDSCL_FULLSCREEN)
656 /* Needs DDSCL_EXCLUSIVE */
657 if(!(cooplevel & DDSCL_EXCLUSIVE) )
659 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
660 LeaveCriticalSection(&ddraw_cs);
661 return DDERR_INVALIDPARAMS;
666 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
667 return DDERR_INVALIDPARAMS;
671 This->cooperative_level &= ~DDSCL_NORMAL;
673 /* Don't override focus windows or private device windows */
675 !(This->focuswindow) &&
676 !(This->devicewindow) &&
679 HRESULT hr = IWineD3DDevice_AcquireFocusWindow(This->wineD3DDevice, hwnd);
682 ERR("Failed to acquire focus window, hr %#x.\n", hr);
683 LeaveCriticalSection(&ddraw_cs);
686 This->dest_window = hwnd;
689 else if(cooplevel & DDSCL_EXCLUSIVE)
691 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
692 LeaveCriticalSection(&ddraw_cs);
693 return DDERR_INVALIDPARAMS;
696 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
698 /* Don't create a device window if a focus window is set */
699 if( !(This->focuswindow) )
701 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
702 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
703 NULL, NULL, NULL, NULL);
706 ERR("Failed to create window, last error %#x.\n", GetLastError());
707 LeaveCriticalSection(&ddraw_cs);
711 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
712 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
714 This->devicewindow = devicewindow;
715 This->dest_window = devicewindow;
719 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
721 /* Enable thread safety in wined3d */
722 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
725 /* Unhandled flags */
726 if(cooplevel & DDSCL_ALLOWREBOOT)
727 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
728 if(cooplevel & DDSCL_ALLOWMODEX)
729 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
730 if(cooplevel & DDSCL_FPUSETUP)
731 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
733 /* Store the cooperative_level */
734 This->cooperative_level |= cooplevel;
735 TRACE("SetCooperativeLevel retuning DD_OK\n");
736 LeaveCriticalSection(&ddraw_cs);
740 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
742 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
744 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw4(iface), window, flags);
747 static HRESULT WINAPI ddraw3_SetCooperativeLevel(IDirectDraw3 *iface, HWND window, DWORD flags)
749 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
751 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw3(iface), window, flags);
754 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
756 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
758 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw2(iface), window, flags);
761 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
763 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
765 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw1(iface), window, flags);
768 /*****************************************************************************
770 * Helper function for SetDisplayMode and RestoreDisplayMode
772 * Implements DirectDraw's SetDisplayMode, but ignores the value of
773 * ForceRefreshRate, since it is already handled by
774 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
775 * without worrying that ForceRefreshRate will override the refresh rate. For
776 * argument and return value documentation, see
777 * ddraw7_SetDisplayMode.
779 *****************************************************************************/
780 static HRESULT ddraw_set_display_mode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
781 DWORD BPP, DWORD RefreshRate, DWORD Flags)
783 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
784 WINED3DDISPLAYMODE Mode;
787 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
788 iface, Width, Height, BPP, RefreshRate, Flags);
790 EnterCriticalSection(&ddraw_cs);
791 if( !Width || !Height )
793 ERR("Width %u, Height %u, what to do?\n", Width, Height);
794 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
795 LeaveCriticalSection(&ddraw_cs);
799 /* Check the exclusive mode
800 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
801 return DDERR_NOEXCLUSIVEMODE;
802 * This is WRONG. Don't know if the SDK is completely
803 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
804 * is returned, but Half-Life 1.1.1.1 (Steam version)
809 Mode.Height = Height;
810 Mode.RefreshRate = RefreshRate;
813 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
814 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
815 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
816 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
817 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
820 /* TODO: The possible return values from msdn suggest that
821 * the screen mode can't be changed if a surface is locked
822 * or some drawing is in progress
825 /* TODO: Lose the primary surface */
826 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
827 0, /* First swapchain */
829 LeaveCriticalSection(&ddraw_cs);
832 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
837 /*****************************************************************************
838 * IDirectDraw7::SetDisplayMode
840 * Sets the display screen resolution, color depth and refresh frequency
841 * when in fullscreen mode (in theory).
842 * Possible return values listed in the SDK suggest that this method fails
843 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
844 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
845 * It seems to be valid to pass 0 for With and Height, this has to be tested
846 * It could mean that the current video mode should be left as-is. (But why
850 * Height, Width: Screen dimension
851 * BPP: Color depth in Bits per pixel
852 * Refreshrate: Screen refresh rate
858 *****************************************************************************/
859 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
860 DWORD BPP, DWORD RefreshRate, DWORD Flags)
862 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
863 iface, Width, Height, BPP, RefreshRate, Flags);
865 if (force_refresh_rate != 0)
867 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
868 RefreshRate, force_refresh_rate);
869 RefreshRate = force_refresh_rate;
872 return ddraw_set_display_mode(iface, Width, Height, BPP, RefreshRate, Flags);
875 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface,
876 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
878 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
879 iface, width, height, bpp, refresh_rate, flags);
881 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw4(iface),
882 width, height, bpp, refresh_rate, flags);
885 static HRESULT WINAPI ddraw3_SetDisplayMode(IDirectDraw3 *iface,
886 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
888 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
889 iface, width, height, bpp, refresh_rate, flags);
891 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw3(iface),
892 width, height, bpp, refresh_rate, flags);
895 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
896 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
898 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
899 iface, width, height, bpp, refresh_rate, flags);
901 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw2(iface),
902 width, height, bpp, refresh_rate, flags);
905 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
907 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
909 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw1(iface), width, height, bpp, 0, 0);
912 /*****************************************************************************
913 * IDirectDraw7::RestoreDisplayMode
915 * Restores the display mode to what it was at creation time. Basically.
917 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
918 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
919 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
920 * -> DD_1 is released. The screen should be left at 1024x768x32.
921 * -> DD_2 is released. The screen should be set to 1400x1050x32
922 * This case is unhandled right now, but Empire Earth does it this way.
923 * (But perhaps there is something in SetCooperativeLevel to prevent this)
925 * The msdn says that this method resets the display mode to what it was before
926 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
930 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
932 *****************************************************************************/
933 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
935 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
937 TRACE("iface %p.\n", iface);
939 return ddraw_set_display_mode(iface, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
942 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
944 TRACE("iface %p.\n", iface);
946 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw4(iface));
949 static HRESULT WINAPI ddraw3_RestoreDisplayMode(IDirectDraw3 *iface)
951 TRACE("iface %p.\n", iface);
953 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw3(iface));
956 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
958 TRACE("iface %p.\n", iface);
960 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw2(iface));
963 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
965 TRACE("iface %p.\n", iface);
967 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw1(iface));
970 /*****************************************************************************
971 * IDirectDraw7::GetCaps
973 * Returns the drives capabilities
975 * Used for version 1, 2, 4 and 7
978 * DriverCaps: Structure to write the Hardware accelerated caps to
979 * HelCaps: Structure to write the emulation caps to
982 * This implementation returns DD_OK only
984 *****************************************************************************/
985 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
987 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
989 WINED3DCAPS winecaps;
991 DDSCAPS2 ddscaps = {0, 0, 0, 0};
993 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
995 /* One structure must be != NULL */
996 if( (!DriverCaps) && (!HELCaps) )
998 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
999 return DDERR_INVALIDPARAMS;
1002 memset(&caps, 0, sizeof(caps));
1003 memset(&winecaps, 0, sizeof(winecaps));
1004 caps.dwSize = sizeof(caps);
1005 EnterCriticalSection(&ddraw_cs);
1006 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
1008 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1009 LeaveCriticalSection(&ddraw_cs);
1013 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1014 LeaveCriticalSection(&ddraw_cs);
1016 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1020 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1021 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1022 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1023 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1024 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1025 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1026 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1027 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1028 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1029 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1030 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1031 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1032 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1033 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1034 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1036 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1039 if(DefaultSurfaceType == SURFACE_GDI) {
1040 caps.dwCaps &= ~DDCAPS_3D;
1041 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1043 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1044 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1045 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1050 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1051 if (TRACE_ON(ddraw))
1053 TRACE("Driver Caps :\n");
1054 DDRAW_dump_DDCAPS(DriverCaps);
1060 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1061 if (TRACE_ON(ddraw))
1063 TRACE("HEL Caps :\n");
1064 DDRAW_dump_DDCAPS(HELCaps);
1071 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1073 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1075 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw4(iface), driver_caps, hel_caps);
1078 static HRESULT WINAPI ddraw3_GetCaps(IDirectDraw3 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1080 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1082 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw3(iface), driver_caps, hel_caps);
1085 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1087 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1089 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw2(iface), driver_caps, hel_caps);
1092 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1094 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1096 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw1(iface), driver_caps, hel_caps);
1099 /*****************************************************************************
1100 * IDirectDraw7::Compact
1102 * No idea what it does, MSDN says it's not implemented.
1105 * DD_OK, but this is unchecked
1107 *****************************************************************************/
1108 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1110 TRACE("iface %p.\n", iface);
1115 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1117 TRACE("iface %p.\n", iface);
1119 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw4(iface));
1122 static HRESULT WINAPI ddraw3_Compact(IDirectDraw3 *iface)
1124 TRACE("iface %p.\n", iface);
1126 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw3(iface));
1129 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1131 TRACE("iface %p.\n", iface);
1133 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw2(iface));
1136 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1138 TRACE("iface %p.\n", iface);
1140 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw1(iface));
1143 /*****************************************************************************
1144 * IDirectDraw7::GetDisplayMode
1146 * Returns information about the current display mode
1148 * Exists in Version 1, 2, 4 and 7
1151 * DDSD: Address of a surface description structure to write the info to
1156 *****************************************************************************/
1157 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1159 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1161 WINED3DDISPLAYMODE Mode;
1164 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1166 EnterCriticalSection(&ddraw_cs);
1167 /* This seems sane */
1170 LeaveCriticalSection(&ddraw_cs);
1171 return DDERR_INVALIDPARAMS;
1174 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1175 * so one method can be used for all versions (Hopefully)
1177 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1178 0 /* swapchain 0 */,
1182 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1183 LeaveCriticalSection(&ddraw_cs);
1187 Size = DDSD->dwSize;
1188 memset(DDSD, 0, Size);
1190 DDSD->dwSize = Size;
1191 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1192 DDSD->dwWidth = Mode.Width;
1193 DDSD->dwHeight = Mode.Height;
1194 DDSD->u2.dwRefreshRate = 60;
1195 DDSD->ddsCaps.dwCaps = 0;
1196 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1197 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
1198 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1202 TRACE("Returning surface desc :\n");
1203 DDRAW_dump_surface_desc(DDSD);
1206 LeaveCriticalSection(&ddraw_cs);
1210 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1212 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1214 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw4(iface), surface_desc);
1217 static HRESULT WINAPI ddraw3_GetDisplayMode(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc)
1219 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1221 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw3(iface), (DDSURFACEDESC2 *)surface_desc);
1224 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1226 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1228 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw2(iface), (DDSURFACEDESC2 *)surface_desc);
1231 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1233 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1235 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw1(iface), (DDSURFACEDESC2 *)surface_desc);
1238 /*****************************************************************************
1239 * IDirectDraw7::GetFourCCCodes
1241 * Returns an array of supported FourCC codes.
1243 * Exists in Version 1, 2, 4 and 7
1246 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1247 * of enumerated codes
1248 * Codes: Pointer to an array of DWORDs where the supported codes are written
1252 * Always returns DD_OK, as it's a stub for now
1254 *****************************************************************************/
1255 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1257 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1258 static const enum wined3d_format_id formats[] =
1260 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1261 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1262 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1264 DWORD count = 0, i, outsize;
1266 WINED3DDISPLAYMODE d3ddm;
1267 WINED3DSURFTYPE type = This->ImplType;
1269 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1271 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1272 0 /* swapchain 0 */,
1275 outsize = NumCodes && Codes ? *NumCodes : 0;
1277 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
1279 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
1280 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
1281 WINED3DADAPTER_DEFAULT,
1283 d3ddm.Format /* AdapterFormat */,
1285 WINED3DRTYPE_SURFACE,
1289 if(count < outsize) {
1290 Codes[count] = formats[i];
1296 TRACE("Returning %u FourCC codes\n", count);
1303 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1305 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1307 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw4(iface), codes_count, codes);
1310 static HRESULT WINAPI ddraw3_GetFourCCCodes(IDirectDraw3 *iface, DWORD *codes_count, DWORD *codes)
1312 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1314 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw3(iface), codes_count, codes);
1317 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1319 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1321 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw2(iface), codes_count, codes);
1324 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1326 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1328 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw1(iface), codes_count, codes);
1331 /*****************************************************************************
1332 * IDirectDraw7::GetMonitorFrequency
1334 * Returns the monitor's frequency
1336 * Exists in Version 1, 2, 4 and 7
1339 * Freq: Pointer to a DWORD to write the frequency to
1342 * Always returns DD_OK
1344 *****************************************************************************/
1345 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1347 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1349 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1350 * but for now this should make the games happy
1356 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1358 TRACE("iface %p, frequency %p.\n", iface, frequency);
1360 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw4(iface), frequency);
1363 static HRESULT WINAPI ddraw3_GetMonitorFrequency(IDirectDraw3 *iface, DWORD *frequency)
1365 TRACE("iface %p, frequency %p.\n", iface, frequency);
1367 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw3(iface), frequency);
1370 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1372 TRACE("iface %p, frequency %p.\n", iface, frequency);
1374 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw2(iface), frequency);
1377 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1379 TRACE("iface %p, frequency %p.\n", iface, frequency);
1381 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw1(iface), frequency);
1384 /*****************************************************************************
1385 * IDirectDraw7::GetVerticalBlankStatus
1387 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1388 * too basically, but as it's a semi stub, I didn't create a function there
1391 * status: Pointer to a BOOL to be filled with the vertical blank status
1395 * DDERR_INVALIDPARAMS if status is NULL
1397 *****************************************************************************/
1398 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1400 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1402 TRACE("iface %p, status %p.\n", iface, status);
1404 /* This looks sane, the MSDN suggests it too */
1405 EnterCriticalSection(&ddraw_cs);
1408 LeaveCriticalSection(&ddraw_cs);
1409 return DDERR_INVALIDPARAMS;
1412 *status = This->fake_vblank;
1413 This->fake_vblank = !This->fake_vblank;
1414 LeaveCriticalSection(&ddraw_cs);
1418 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1420 TRACE("iface %p, status %p.\n", iface, status);
1422 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw4(iface), status);
1425 static HRESULT WINAPI ddraw3_GetVerticalBlankStatus(IDirectDraw3 *iface, BOOL *status)
1427 TRACE("iface %p, status %p.\n", iface, status);
1429 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw3(iface), status);
1432 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1434 TRACE("iface %p, status %p.\n", iface, status);
1436 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw2(iface), status);
1439 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1441 TRACE("iface %p, status %p.\n", iface, status);
1443 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw1(iface), status);
1446 /*****************************************************************************
1447 * IDirectDraw7::GetAvailableVidMem
1449 * Returns the total and free video memory
1452 * Caps: Specifies the memory type asked for
1453 * total: Pointer to a DWORD to be filled with the total memory
1454 * free: Pointer to a DWORD to be filled with the free memory
1458 * DDERR_INVALIDPARAMS of free and total are NULL
1460 *****************************************************************************/
1461 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1463 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1465 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1469 TRACE("(%p) Asked for memory with description: ", This);
1470 DDRAW_dump_DDSCAPS2(Caps);
1472 EnterCriticalSection(&ddraw_cs);
1474 /* Todo: System memory vs local video memory vs non-local video memory
1475 * The MSDN also mentions differences between texture memory and other
1476 * resources, but that's not important
1479 if( (!total) && (!free) )
1481 LeaveCriticalSection(&ddraw_cs);
1482 return DDERR_INVALIDPARAMS;
1485 if(total) *total = This->total_vidmem;
1486 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1488 LeaveCriticalSection(&ddraw_cs);
1492 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1493 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1495 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1497 return ddraw7_GetAvailableVidMem((IDirectDraw7 *)ddraw_from_ddraw4(iface), caps, total, free);
1500 static HRESULT WINAPI ddraw3_GetAvailableVidMem(IDirectDraw3 *iface,
1501 DDSCAPS *caps, DWORD *total, DWORD *free)
1505 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1507 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1508 return ddraw7_GetAvailableVidMem((IDirectDraw7 *)ddraw_from_ddraw3(iface), &caps2, total, free);
1511 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1512 DDSCAPS *caps, DWORD *total, DWORD *free)
1516 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1518 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1519 return ddraw7_GetAvailableVidMem((IDirectDraw7 *)ddraw_from_ddraw2(iface), &caps2, total, free);
1522 /*****************************************************************************
1523 * IDirectDraw7::Initialize
1525 * Initializes a DirectDraw interface.
1528 * GUID: Interface identifier. Well, don't know what this is really good
1532 * Returns DD_OK on the first call,
1533 * DDERR_ALREADYINITIALIZED on repeated calls
1535 *****************************************************************************/
1536 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *Guid)
1538 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1540 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(Guid));
1542 if(This->initialized)
1544 return DDERR_ALREADYINITIALIZED;
1552 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1554 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1556 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw4(iface), guid);
1559 static HRESULT WINAPI ddraw3_Initialize(IDirectDraw3 *iface, GUID *guid)
1561 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1563 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw3(iface), guid);
1566 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1568 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1570 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw2(iface), guid);
1573 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1575 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1577 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw1(iface), guid);
1580 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1582 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1587 /*****************************************************************************
1588 * IDirectDraw7::FlipToGDISurface
1590 * "Makes the surface that the GDI writes to the primary surface"
1591 * Looks like some windows specific thing we don't have to care about.
1592 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1593 * show error boxes ;)
1594 * Well, just return DD_OK.
1597 * Always returns DD_OK
1599 *****************************************************************************/
1600 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1602 FIXME("iface %p stub!\n", iface);
1607 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1609 TRACE("iface %p.\n", iface);
1611 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw4(iface));
1614 static HRESULT WINAPI ddraw3_FlipToGDISurface(IDirectDraw3 *iface)
1616 TRACE("iface %p.\n", iface);
1618 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw3(iface));
1621 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1623 TRACE("iface %p.\n", iface);
1625 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw2(iface));
1628 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1630 TRACE("iface %p.\n", iface);
1632 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw1(iface));
1635 /*****************************************************************************
1636 * IDirectDraw7::WaitForVerticalBlank
1638 * This method allows applications to get in sync with the vertical blank
1640 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1641 * redraw the screen, most likely because of this stub
1644 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1645 * or DDWAITVB_BLOCKEND
1646 * h: Not used, according to MSDN
1649 * Always returns DD_OK
1651 *****************************************************************************/
1652 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1656 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1658 /* This function is called often, so print the fixme only once */
1661 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1665 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1666 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1667 return DDERR_UNSUPPORTED; /* unchecked */
1672 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1674 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1676 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw4(iface), flags, event);
1679 static HRESULT WINAPI ddraw3_WaitForVerticalBlank(IDirectDraw3 *iface, DWORD flags, HANDLE event)
1681 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1683 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags, event);
1686 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1688 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1690 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags, event);
1693 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1695 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1697 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags, event);
1700 /*****************************************************************************
1701 * IDirectDraw7::GetScanLine
1703 * Returns the scan line that is being drawn on the monitor
1706 * Scanline: Address to write the scan line value to
1709 * Always returns DD_OK
1711 *****************************************************************************/
1712 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1714 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1715 static BOOL hide = FALSE;
1716 WINED3DDISPLAYMODE Mode;
1718 TRACE("iface %p, line %p.\n", iface, Scanline);
1720 /* This function is called often, so print the fixme only once */
1721 EnterCriticalSection(&ddraw_cs);
1724 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1728 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1732 /* Fake the line sweeping of the monitor */
1733 /* FIXME: We should synchronize with a source to keep the refresh rate */
1734 *Scanline = This->cur_scanline++;
1735 /* Assume 20 scan lines in the vertical blank */
1736 if (This->cur_scanline >= Mode.Height + 20)
1737 This->cur_scanline = 0;
1739 LeaveCriticalSection(&ddraw_cs);
1743 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1745 TRACE("iface %p, line %p.\n", iface, line);
1747 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw4(iface), line);
1750 static HRESULT WINAPI ddraw3_GetScanLine(IDirectDraw3 *iface, DWORD *line)
1752 TRACE("iface %p, line %p.\n", iface, line);
1754 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw3(iface), line);
1757 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1759 TRACE("iface %p, line %p.\n", iface, line);
1761 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw2(iface), line);
1764 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1766 TRACE("iface %p, line %p.\n", iface, line);
1768 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw1(iface), line);
1771 /*****************************************************************************
1772 * IDirectDraw7::TestCooperativeLevel
1774 * Informs the application about the state of the video adapter, depending
1775 * on the cooperative level
1778 * DD_OK if the device is in a sane state
1779 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1780 * if the state is not correct(See below)
1782 *****************************************************************************/
1783 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1785 TRACE("iface %p.\n", iface);
1790 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1792 TRACE("iface %p.\n", iface);
1794 return ddraw7_TestCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw4(iface));
1797 /*****************************************************************************
1798 * IDirectDraw7::GetGDISurface
1800 * Returns the surface that GDI is treating as the primary surface.
1801 * For Wine this is the front buffer
1804 * GDISurface: Address to write the surface pointer to
1807 * DD_OK if the surface was found
1808 * DDERR_NOTFOUND if the GDI surface wasn't found
1810 *****************************************************************************/
1811 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1813 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1814 IWineD3DSurface *Surf;
1815 IDirectDrawSurface7 *ddsurf;
1819 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1821 /* Get the back buffer from the wineD3DDevice and search its
1822 * attached surfaces for the front buffer
1824 EnterCriticalSection(&ddraw_cs);
1825 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1827 0, /* first back buffer*/
1828 WINED3DBACKBUFFER_TYPE_MONO,
1831 if( (hr != D3D_OK) ||
1834 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1835 LeaveCriticalSection(&ddraw_cs);
1836 return DDERR_NOTFOUND;
1839 ddsurf = IWineD3DSurface_GetParent(Surf);
1840 IWineD3DSurface_Release(Surf);
1842 /* Find the front buffer */
1843 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1844 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1849 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1852 /* The AddRef is OK this time */
1853 LeaveCriticalSection(&ddraw_cs);
1857 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
1859 TRACE("iface %p, surface %p.\n", iface, surface);
1861 return ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw4(iface), (IDirectDrawSurface7 **)surface);
1864 static HRESULT WINAPI ddraw3_GetGDISurface(IDirectDraw3 *iface, IDirectDrawSurface **surface)
1866 IDirectDrawSurface7 *surface7;
1869 TRACE("iface %p, surface %p.\n", iface, surface);
1871 hr = ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw3(iface), &surface7);
1872 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
1877 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
1879 IDirectDrawSurface7 *surface7;
1882 TRACE("iface %p, surface %p.\n", iface, surface);
1884 hr = ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw2(iface), &surface7);
1885 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
1890 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
1892 IDirectDrawSurface7 *surface7;
1895 TRACE("iface %p, surface %p.\n", iface, surface);
1897 hr = ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw1(iface), &surface7);
1898 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
1903 struct displaymodescallback_context
1905 LPDDENUMMODESCALLBACK func;
1909 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
1911 struct displaymodescallback_context *cbcontext = context;
1914 memcpy(&desc, surface_desc, sizeof(desc));
1915 desc.dwSize = sizeof(desc);
1917 return cbcontext->func(&desc, cbcontext->context);
1920 /*****************************************************************************
1921 * IDirectDraw7::EnumDisplayModes
1923 * Enumerates the supported Display modes. The modes can be filtered with
1924 * the DDSD parameter.
1927 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1928 * DDSD: Surface description to filter the modes
1929 * Context: Pointer passed back to the callback function
1930 * cb: Application-provided callback function
1934 * DDERR_INVALIDPARAMS if the callback wasn't set
1936 *****************************************************************************/
1937 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
1938 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
1940 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1941 unsigned int modenum, fmt;
1942 enum wined3d_format_id pixelformat = WINED3DFMT_UNKNOWN;
1943 WINED3DDISPLAYMODE mode;
1944 DDSURFACEDESC2 callback_sd;
1945 WINED3DDISPLAYMODE *enum_modes = NULL;
1946 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1948 static const enum wined3d_format_id checkFormatList[] =
1950 WINED3DFMT_B8G8R8X8_UNORM,
1951 WINED3DFMT_B5G6R5_UNORM,
1955 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
1956 iface, Flags, DDSD, Context, cb);
1958 EnterCriticalSection(&ddraw_cs);
1959 /* This looks sane */
1962 LeaveCriticalSection(&ddraw_cs);
1963 return DDERR_INVALIDPARAMS;
1968 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1969 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1972 if(!(Flags & DDEDM_REFRESHRATES))
1974 enum_mode_array_size = 16;
1975 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1978 ERR("Out of memory\n");
1979 LeaveCriticalSection(&ddraw_cs);
1980 return DDERR_OUTOFMEMORY;
1984 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1986 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1992 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1993 WINED3DADAPTER_DEFAULT,
1994 checkFormatList[fmt],
1996 &mode) == WINED3D_OK)
2000 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
2001 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
2004 if(!(Flags & DDEDM_REFRESHRATES))
2006 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2007 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2008 * to be reduced to a single unique result in such case.
2013 for (i = 0; i < enum_mode_count; i++)
2015 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
2016 enum_modes[i].Format == mode.Format)
2026 memset(&callback_sd, 0, sizeof(callback_sd));
2027 callback_sd.dwSize = sizeof(callback_sd);
2028 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2030 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
2031 if(Flags & DDEDM_REFRESHRATES)
2033 callback_sd.dwFlags |= DDSD_REFRESHRATE;
2034 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
2037 callback_sd.dwWidth = mode.Width;
2038 callback_sd.dwHeight = mode.Height;
2040 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
2042 /* Calc pitch and DWORD align like MSDN says */
2043 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
2044 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2046 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2047 callback_sd.u2.dwRefreshRate);
2049 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2051 TRACE("Application asked to terminate the enumeration\n");
2052 HeapFree(GetProcessHeap(), 0, enum_modes);
2053 LeaveCriticalSection(&ddraw_cs);
2057 if(!(Flags & DDEDM_REFRESHRATES))
2059 if (enum_mode_count == enum_mode_array_size)
2061 WINED3DDISPLAYMODE *new_enum_modes;
2063 enum_mode_array_size *= 2;
2064 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2066 if (!new_enum_modes)
2068 ERR("Out of memory\n");
2069 HeapFree(GetProcessHeap(), 0, enum_modes);
2070 LeaveCriticalSection(&ddraw_cs);
2071 return DDERR_OUTOFMEMORY;
2074 enum_modes = new_enum_modes;
2077 enum_modes[enum_mode_count++] = mode;
2082 TRACE("End of enumeration\n");
2083 HeapFree(GetProcessHeap(), 0, enum_modes);
2084 LeaveCriticalSection(&ddraw_cs);
2088 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2089 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2091 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2092 iface, flags, surface_desc, context, callback);
2094 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw4(iface), flags,
2095 surface_desc, context, callback);
2098 static HRESULT WINAPI ddraw3_EnumDisplayModes(IDirectDraw3 *iface, DWORD flags,
2099 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2101 struct displaymodescallback_context cbcontext;
2103 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2104 iface, flags, surface_desc, context, callback);
2106 cbcontext.func = callback;
2107 cbcontext.context = context;
2109 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags,
2110 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumDisplayModesCallbackThunk);
2113 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2114 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2116 struct displaymodescallback_context cbcontext;
2118 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2119 iface, flags, surface_desc, context, callback);
2121 cbcontext.func = callback;
2122 cbcontext.context = context;
2124 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags,
2125 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumDisplayModesCallbackThunk);
2128 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2129 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2131 struct displaymodescallback_context cbcontext;
2133 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2134 iface, flags, surface_desc, context, callback);
2136 cbcontext.func = callback;
2137 cbcontext.context = context;
2139 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags,
2140 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumDisplayModesCallbackThunk);
2143 /*****************************************************************************
2144 * IDirectDraw7::EvaluateMode
2146 * Used with IDirectDraw7::StartModeTest to test video modes.
2147 * EvaluateMode is used to pass or fail a mode, and continue with the next
2151 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2152 * Timeout: Returns the amount of seconds left before the mode would have
2153 * been failed automatically
2156 * This implementation always DD_OK, because it's a stub
2158 *****************************************************************************/
2159 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2161 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2163 /* When implementing this, implement it in WineD3D */
2168 /*****************************************************************************
2169 * IDirectDraw7::GetDeviceIdentifier
2171 * Returns the device identifier, which gives information about the driver
2172 * Our device identifier is defined at the beginning of this file.
2175 * DDDI: Address for the returned structure
2176 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2179 * On success it returns DD_OK
2180 * DDERR_INVALIDPARAMS if DDDI is NULL
2182 *****************************************************************************/
2183 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2184 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2186 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2189 return DDERR_INVALIDPARAMS;
2191 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2192 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2193 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2194 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2195 * bytes too long. So only copy the relevant part of the structure
2198 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2202 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2203 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2205 DDDEVICEIDENTIFIER2 identifier2;
2208 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2210 hr = ddraw7_GetDeviceIdentifier((IDirectDraw7 *)ddraw_from_ddraw4(iface), &identifier2, flags);
2211 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2216 /*****************************************************************************
2217 * IDirectDraw7::GetSurfaceFromDC
2219 * Returns the Surface for a GDI device context handle.
2220 * Is this related to IDirectDrawSurface::GetDC ???
2223 * hdc: hdc to return the surface for
2224 * Surface: Address to write the surface pointer to
2227 * Always returns DD_OK because it's a stub
2229 *****************************************************************************/
2230 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc, IDirectDrawSurface7 **Surface)
2232 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2233 IWineD3DSurface *wined3d_surface;
2236 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2238 if (!Surface) return E_INVALIDARG;
2240 hr = IWineD3DDevice_GetSurfaceFromDC(This->wineD3DDevice, hdc, &wined3d_surface);
2243 TRACE("No surface found for dc %p.\n", hdc);
2245 return DDERR_NOTFOUND;
2248 *Surface = IWineD3DSurface_GetParent(wined3d_surface);
2249 IDirectDrawSurface7_AddRef(*Surface);
2250 TRACE("Returning surface %p.\n", Surface);
2254 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc, IDirectDrawSurface4 **surface)
2256 IDirectDrawSurface7 *surface7;
2259 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2261 if (!surface) return E_INVALIDARG;
2263 hr = ddraw7_GetSurfaceFromDC((IDirectDraw7 *)ddraw_from_ddraw4(iface), dc, &surface7);
2264 *surface = surface7 ? (IDirectDrawSurface4 *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
2269 static HRESULT WINAPI ddraw3_GetSurfaceFromDC(IDirectDraw3 *iface, HDC dc, IDirectDrawSurface **surface)
2271 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2273 return ddraw7_GetSurfaceFromDC((IDirectDraw7 *)ddraw_from_ddraw3(iface),
2274 dc, (IDirectDrawSurface7 **)surface);
2277 /*****************************************************************************
2278 * IDirectDraw7::RestoreAllSurfaces
2280 * Calls the restore method of all surfaces
2285 * Always returns DD_OK because it's a stub
2287 *****************************************************************************/
2288 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2290 FIXME("iface %p stub!\n", iface);
2292 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2293 * get their parent and call their restore method. Do not implement
2294 * it in WineD3D, as restoring a surface means re-creating the
2300 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2302 TRACE("iface %p.\n", iface);
2304 return ddraw7_RestoreAllSurfaces((IDirectDraw7 *)ddraw_from_ddraw4(iface));
2307 /*****************************************************************************
2308 * IDirectDraw7::StartModeTest
2310 * Tests the specified video modes to update the system registry with
2311 * refresh rate information. StartModeTest starts the mode test,
2312 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2313 * isn't called within 15 seconds, the mode is failed automatically
2315 * As refresh rates are handled by the X server, I don't think this
2316 * Method is important
2319 * Modes: An array of mode specifications
2320 * NumModes: The number of modes in Modes
2321 * Flags: Some flags...
2324 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2325 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2328 *****************************************************************************/
2329 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2331 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2332 iface, Modes, NumModes, Flags);
2334 /* This looks sane */
2335 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2337 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2338 * As it is not, DDERR_TESTFINISHED is returned
2339 * (hopefully that's correct
2341 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2342 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2348 /*****************************************************************************
2349 * ddraw_recreate_surfaces_cb
2351 * Enumeration callback for ddraw_recreate_surface.
2352 * It re-recreates the WineD3DSurface. It's pretty straightforward
2354 *****************************************************************************/
2355 HRESULT WINAPI ddraw_recreate_surfaces_cb(IDirectDrawSurface7 *surf, DDSURFACEDESC2 *desc, void *Context)
2357 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
2358 IDirectDrawImpl *This = surfImpl->ddraw;
2359 IWineD3DSurface *wineD3DSurface;
2360 IWineD3DSwapChain *swapchain;
2363 IWineD3DClipper *clipper = NULL;
2365 WINED3DSURFACE_DESC Desc;
2366 enum wined3d_format_id Format;
2370 WINED3DMULTISAMPLE_TYPE MultiSampleType;
2371 DWORD MultiSampleQuality;
2375 TRACE("surface %p, surface_desc %p, context %p.\n",
2376 surf, desc, Context);
2378 /* For the enumeration */
2379 IDirectDrawSurface7_Release(surf);
2381 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
2383 /* Get the objects */
2384 swapchain = surfImpl->wineD3DSwapChain;
2385 surfImpl->wineD3DSwapChain = NULL;
2386 wineD3DSurface = surfImpl->WineD3DSurface;
2388 /* get the clipper */
2389 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
2391 /* Get the surface properties */
2392 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
2393 if(hr != D3D_OK) return hr;
2395 Format = Desc.format;
2398 MultiSampleType = Desc.multisample_type;
2399 MultiSampleQuality = Desc.multisample_quality;
2401 Height = Desc.height;
2403 parent = IWineD3DSurface_GetParent(wineD3DSurface);
2404 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format, TRUE /* Lockable */,
2405 FALSE /* Discard */, surfImpl->mipmap_level, Usage, Pool, MultiSampleType, MultiSampleQuality,
2406 This->ImplType, parent, &ddraw_null_wined3d_parent_ops, &surfImpl->WineD3DSurface);
2409 surfImpl->WineD3DSurface = wineD3DSurface;
2413 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
2415 /* TODO: Copy the surface content, except for render targets */
2417 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
2421 /* The backbuffers have the swapchain set as well, but the primary
2422 * owns it and destroys it
2424 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2425 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2427 surfImpl->isRenderTarget = FALSE;
2429 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
2430 TRACE("Surface released successful, next surface\n");
2432 ERR("Something's still holding the old WineD3DSurface\n");
2435 surfImpl->ImplType = This->ImplType;
2439 IWineD3DClipper_Release(clipper);
2441 return DDENUMRET_OK;
2444 /*****************************************************************************
2445 * ddraw_recreate_surfaces
2447 * A function, that converts all wineD3DSurfaces to the new implementation type
2448 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
2449 * new WineD3DSurface, copies the content and releases the old surface
2451 *****************************************************************************/
2452 static HRESULT ddraw_recreate_surfaces(IDirectDrawImpl *This)
2454 DDSURFACEDESC2 desc;
2455 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
2457 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
2459 /* Should happen almost never */
2460 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
2462 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2464 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
2466 memset(&desc, 0, sizeof(desc));
2467 desc.dwSize = sizeof(desc);
2469 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, ddraw_recreate_surfaces_cb);
2472 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain)
2474 IUnknown *swapChainParent;
2476 TRACE("swapchain %p.\n", pSwapChain);
2478 swapChainParent = IWineD3DSwapChain_GetParent(pSwapChain);
2479 return IUnknown_Release(swapChainParent);
2482 /*****************************************************************************
2483 * ddraw_create_surface
2485 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2486 * with the passed parameters.
2489 * DDSD: Description of the surface to create
2490 * Surf: Address to store the interface pointer at
2495 *****************************************************************************/
2496 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2497 IDirectDrawSurfaceImpl **ppSurf, UINT level)
2499 WINED3DSURFTYPE ImplType = This->ImplType;
2502 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2503 This, pDDSD, ppSurf, level);
2505 if (TRACE_ON(ddraw))
2507 TRACE(" (%p) Requesting surface desc :\n", This);
2508 DDRAW_dump_surface_desc(pDDSD);
2511 /* Select the surface type, if it wasn't choosen yet */
2512 if(ImplType == SURFACE_UNKNOWN)
2514 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
2515 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2517 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
2518 ImplType = SURFACE_OPENGL;
2521 /* Otherwise use GDI surfaces for now */
2524 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
2525 ImplType = SURFACE_GDI;
2528 /* Policy if all surface implementations are available:
2529 * First, check if a default type was set with winecfg. If not,
2530 * try Xrender surfaces, and use them if they work. Next, check if
2531 * accelerated OpenGL is available, and use GL surfaces in this
2532 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
2533 * was created, always use OpenGL surfaces.
2535 * (Note: Xrender surfaces are not implemented for now, the
2536 * unaccelerated implementation uses GDI to render in Software)
2539 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
2540 * be re-created. This could be done with IDirectDrawSurface7::Restore
2542 This->ImplType = ImplType;
2546 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2547 && (This->ImplType != SURFACE_OPENGL)
2548 && DefaultSurfaceType == SURFACE_UNKNOWN)
2550 /* We have to change to OpenGL,
2551 * and re-create all WineD3DSurfaces
2553 ImplType = SURFACE_OPENGL;
2554 This->ImplType = ImplType;
2555 TRACE("(%p) Re-creating all surfaces\n", This);
2556 ddraw_recreate_surfaces(This);
2557 TRACE("(%p) Done recreating all surfaces\n", This);
2559 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2561 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2562 /* Do not fail surface creation, only fail 3D device creation */
2566 /* Create the Surface object */
2567 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2570 ERR("(%p) Error allocating memory for a surface\n", This);
2571 return DDERR_OUTOFVIDEOMEMORY;
2574 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, ImplType);
2577 WARN("Failed to initialize surface, hr %#x.\n", hr);
2578 HeapFree(GetProcessHeap(), 0, *ppSurf);
2582 /* Increase the surface counter, and attach the surface */
2583 InterlockedIncrement(&This->surfaces);
2584 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2586 TRACE("Created surface %p.\n", *ppSurf);
2590 /*****************************************************************************
2591 * CreateAdditionalSurfaces
2593 * Creates a new mipmap chain.
2596 * root: Root surface to attach the newly created chain to
2597 * count: number of surfaces to create
2598 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2599 * effects on the caller
2600 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2601 * creates an additional surface without the mipmapping flags
2603 *****************************************************************************/
2605 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2606 IDirectDrawSurfaceImpl *root,
2608 DDSURFACEDESC2 DDSD,
2611 UINT i, j, level = 0;
2613 IDirectDrawSurfaceImpl *last = root;
2615 for(i = 0; i < count; i++)
2617 IDirectDrawSurfaceImpl *object2 = NULL;
2619 /* increase the mipmap level, but only if a mipmap is created
2620 * In this case, also halve the size
2622 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2625 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2626 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2627 /* Set the mipmap sublevel flag according to msdn */
2628 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2632 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2634 CubeFaceRoot = FALSE;
2636 hr = ddraw_create_surface(This, &DDSD, &object2, level);
2642 /* Add the new surface to the complex attachment array */
2643 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2645 if(last->complex_array[j]) continue;
2646 last->complex_array[j] = object2;
2651 /* Remove the (possible) back buffer cap from the new surface description,
2652 * because only one surface in the flipping chain is a back buffer, one
2653 * is a front buffer, the others are just primary surfaces.
2655 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2660 /* Must set all attached surfaces (e.g. mipmaps) versions as well */
2661 static void ddraw_set_surface_version(IDirectDrawSurfaceImpl *surface, UINT version)
2665 TRACE("surface %p, version %u -> %u.\n", surface, surface->version, version);
2667 surface->version = version;
2668 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
2670 if (!surface->complex_array[i]) break;
2671 ddraw_set_surface_version(surface->complex_array[i], version);
2673 while ((surface = surface->next_attached))
2675 ddraw_set_surface_version(surface, version);
2679 /*****************************************************************************
2680 * ddraw_attach_d3d_device
2682 * Initializes the D3D capabilities of WineD3D
2685 * primary: The primary surface for D3D
2691 *****************************************************************************/
2692 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2694 WINED3DPRESENT_PARAMETERS localParameters;
2695 HWND window = ddraw->dest_window;
2698 TRACE("ddraw %p, primary %p.\n", ddraw, primary);
2700 if (!window || window == GetDesktopWindow())
2702 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2703 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2704 NULL, NULL, NULL, NULL);
2707 ERR("Failed to create window, last error %#x.\n", GetLastError());
2711 ShowWindow(window, SW_HIDE); /* Just to be sure */
2712 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2716 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2718 ddraw->d3d_window = window;
2720 /* Store the future Render Target surface */
2721 ddraw->d3d_target = primary;
2723 /* Use the surface description for the device parameters, not the device
2724 * settings. The application might render to an offscreen surface. */
2725 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2726 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2727 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2728 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2729 ? primary->surface_desc.dwBackBufferCount : 0;
2730 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2731 localParameters.MultiSampleQuality = 0;
2732 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2733 localParameters.hDeviceWindow = window;
2734 localParameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2735 localParameters.EnableAutoDepthStencil = TRUE;
2736 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2737 localParameters.Flags = 0;
2738 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2739 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2741 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2742 * recursive loop until ram or emulated video memory is full. */
2743 ddraw->d3d_initialized = TRUE;
2744 hr = IWineD3DDevice_Init3D(ddraw->wineD3DDevice, &localParameters);
2747 ddraw->d3d_target = NULL;
2748 ddraw->d3d_initialized = FALSE;
2752 ddraw->declArraySize = 2;
2753 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2756 ERR("Error allocating an array for the converted vertex decls.\n");
2757 ddraw->declArraySize = 0;
2758 hr = IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
2759 return E_OUTOFMEMORY;
2762 TRACE("Successfully initialized 3D.\n");
2767 static HRESULT ddraw_create_gdi_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2769 WINED3DPRESENT_PARAMETERS presentation_parameters;
2773 window = ddraw->dest_window;
2775 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2777 /* Use the surface description for the device parameters, not the device
2778 * settings. The application might render to an offscreen surface. */
2779 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2780 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2781 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2782 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2783 ? primary->surface_desc.dwBackBufferCount : 0;
2784 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2785 presentation_parameters.MultiSampleQuality = 0;
2786 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2787 presentation_parameters.hDeviceWindow = window;
2788 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2789 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2790 presentation_parameters.AutoDepthStencilFormat = 0;
2791 presentation_parameters.Flags = 0;
2792 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2793 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2795 ddraw->d3d_target = primary;
2796 hr = IWineD3DDevice_InitGDI(ddraw->wineD3DDevice, &presentation_parameters);
2797 ddraw->d3d_target = NULL;
2800 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
2801 primary->wineD3DSwapChain = NULL;
2807 /*****************************************************************************
2808 * IDirectDraw7::CreateSurface
2810 * Creates a new IDirectDrawSurface object and returns its interface.
2812 * The surface connections with wined3d are a bit tricky. Basically it works
2815 * |------------------------| |-----------------|
2816 * | DDraw surface | | WineD3DSurface |
2818 * | WineD3DSurface |-------------->| |
2819 * | Child |<------------->| Parent |
2820 * |------------------------| |-----------------|
2822 * The DDraw surface is the parent of the wined3d surface, and it releases
2823 * the WineD3DSurface when the ddraw surface is destroyed.
2825 * However, for all surfaces which can be in a container in WineD3D,
2826 * we have to do this. These surfaces are usually complex surfaces,
2827 * so this concerns primary surfaces with a front and a back buffer,
2830 * |------------------------| |-----------------|
2831 * | DDraw surface | | Container |
2833 * | Child |<------------->| Parent |
2834 * | Texture |<------------->| |
2835 * | WineD3DSurface |<----| | Levels |<--|
2836 * | Complex connection | | | | |
2837 * |------------------------| | |-----------------| |
2841 * | |------------------| | |-----------------| |
2842 * | | IParent | |-------->| WineD3DSurface | |
2844 * | | Child |<------------->| Parent | |
2845 * | | | | Container |<--|
2846 * | |------------------| |-----------------| |
2848 * | |----------------------| |
2849 * | | DDraw surface 2 | |
2851 * |<->| Complex root Child | |
2853 * | | WineD3DSurface |<----| |
2854 * | |----------------------| | |
2856 * | |---------------------| | |-----------------| |
2857 * | | IParent | |----->| WineD3DSurface | |
2859 * | | Child |<---------->| Parent | |
2860 * | |---------------------| | Container |<--|
2861 * | |-----------------| |
2863 * | ---More surfaces can follow--- |
2865 * The reason is that the IWineD3DSwapchain(render target container)
2866 * and the IWineD3DTexure(Texture container) release the parents
2867 * of their surface's children, but by releasing the complex root
2868 * the surfaces which are complexly attached to it are destroyed
2869 * too. See IDirectDrawSurface::Release for a more detailed
2873 * DDSD: Description of the surface to create
2874 * Surf: Address to store the interface pointer at
2875 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2876 * aggregation, so it has to be NULL
2880 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2881 * DDERR_* if an error occurs
2883 *****************************************************************************/
2884 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface,
2885 DDSURFACEDESC2 *DDSD, IDirectDrawSurface7 **Surf, IUnknown *UnkOuter)
2887 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2888 IDirectDrawSurfaceImpl *object = NULL;
2890 LONG extra_surfaces = 0;
2891 DDSURFACEDESC2 desc2;
2892 WINED3DDISPLAYMODE Mode;
2893 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2895 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2896 iface, DDSD, Surf, UnkOuter);
2898 /* Some checks before we start */
2899 if (TRACE_ON(ddraw))
2901 TRACE(" (%p) Requesting surface desc :\n", This);
2902 DDRAW_dump_surface_desc(DDSD);
2904 EnterCriticalSection(&ddraw_cs);
2906 if (UnkOuter != NULL)
2908 FIXME("(%p) : outer != NULL?\n", This);
2909 LeaveCriticalSection(&ddraw_cs);
2910 return CLASS_E_NOAGGREGATION; /* unchecked */
2915 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2916 LeaveCriticalSection(&ddraw_cs);
2917 return E_POINTER; /* unchecked */
2920 if (!(DDSD->dwFlags & DDSD_CAPS))
2922 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2923 DDSD->dwFlags |= DDSD_CAPS;
2926 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2928 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2929 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2932 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2934 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2935 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2936 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2939 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2940 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2942 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2944 LeaveCriticalSection(&ddraw_cs);
2945 return DDERR_NOEXCLUSIVEMODE;
2948 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2949 WARN("Application tried to create an explicit front or back buffer\n");
2950 LeaveCriticalSection(&ddraw_cs);
2951 return DDERR_INVALIDCAPS;
2954 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2956 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2957 WARN("Application tries to put the surface in both system and video memory\n");
2958 LeaveCriticalSection(&ddraw_cs);
2960 return DDERR_INVALIDCAPS;
2963 /* Check cube maps but only if the size includes them */
2964 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2966 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2967 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2969 WARN("Cube map faces requested without cube map flag\n");
2970 LeaveCriticalSection(&ddraw_cs);
2971 return DDERR_INVALIDCAPS;
2973 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2974 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2976 WARN("Cube map without faces requested\n");
2977 LeaveCriticalSection(&ddraw_cs);
2978 return DDERR_INVALIDPARAMS;
2981 /* Quick tests confirm those can be created, but we don't do that yet */
2982 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2983 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2985 FIXME("Partial cube maps not supported yet\n");
2989 /* According to the msdn this flag is ignored by CreateSurface */
2990 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2991 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2993 /* Modify some flags */
2994 memset(&desc2, 0, sizeof(desc2));
2995 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2996 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2997 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2998 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
3000 /* Get the video mode from WineD3D - we will need it */
3001 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
3002 0, /* Swapchain 0 */
3006 ERR("Failed to read display mode from wined3d\n");
3007 switch(This->orig_bpp)
3010 Mode.Format = WINED3DFMT_P8_UINT;
3014 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
3018 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
3022 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
3026 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
3029 Mode.Width = This->orig_width;
3030 Mode.Height = This->orig_height;
3033 /* No pixelformat given? Use the current screen format */
3034 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
3036 desc2.dwFlags |= DDSD_PIXELFORMAT;
3037 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
3039 /* Wait: It could be a Z buffer */
3040 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3042 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
3045 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
3048 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
3051 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
3054 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
3057 ERR("Unknown Z buffer bit depth\n");
3062 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
3066 /* No Width or no Height? Use the original screen size
3068 if(!(desc2.dwFlags & DDSD_WIDTH) ||
3069 !(desc2.dwFlags & DDSD_HEIGHT) )
3071 /* Invalid for non-render targets */
3072 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3074 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
3076 LeaveCriticalSection(&ddraw_cs);
3077 return DDERR_INVALIDPARAMS;
3080 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
3081 desc2.dwWidth = Mode.Width;
3082 desc2.dwHeight = Mode.Height;
3085 /* Mipmap count fixes */
3086 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3088 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
3090 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
3092 /* Mipmap count is given, should not be 0 */
3093 if( desc2.u2.dwMipMapCount == 0 )
3095 LeaveCriticalSection(&ddraw_cs);
3096 return DDERR_INVALIDPARAMS;
3101 /* Undocumented feature: Create sublevels until
3102 * either the width or the height is 1
3104 DWORD min = desc2.dwWidth < desc2.dwHeight ?
3105 desc2.dwWidth : desc2.dwHeight;
3106 desc2.u2.dwMipMapCount = 0;
3109 desc2.u2.dwMipMapCount += 1;
3116 /* Not-complex mipmap -> Mipmapcount = 1 */
3117 desc2.u2.dwMipMapCount = 1;
3119 extra_surfaces = desc2.u2.dwMipMapCount - 1;
3121 /* There's a mipmap count in the created surface in any case */
3122 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
3124 /* If no mipmap is given, the texture has only one level */
3126 /* The first surface is a front buffer, the back buffer is created afterwards */
3127 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
3129 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
3132 /* The root surface in a cube map is positive x */
3133 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3135 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3136 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3139 /* Create the first surface */
3140 hr = ddraw_create_surface(This, &desc2, &object, 0);
3143 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
3144 LeaveCriticalSection(&ddraw_cs);
3147 object->is_complex_root = TRUE;
3149 *Surf = (IDirectDrawSurface7 *)object;
3151 /* Create Additional surfaces if necessary
3152 * This applies to Primary surfaces which have a back buffer count
3153 * set, but not to mipmap textures. In case of Mipmap textures,
3154 * wineD3D takes care of the creation of additional surfaces
3156 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
3158 extra_surfaces = DDSD->dwBackBufferCount;
3159 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
3160 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
3161 desc2.dwBackBufferCount = 0;
3165 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3167 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3168 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
3169 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
3170 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
3171 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
3172 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
3173 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
3174 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
3175 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
3176 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
3177 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
3178 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
3179 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
3180 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
3181 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
3182 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
3183 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3186 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
3189 /* This destroys and possibly created surfaces too */
3190 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
3191 LeaveCriticalSection(&ddraw_cs);
3195 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
3196 * But attach the d3ddevice only if the currently created surface was
3197 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
3198 * The only case I can think of where this doesn't apply is when a
3199 * 2D app was configured by the user to run with OpenGL and it didn't create
3200 * the render target as first surface. In this case the render target creation
3201 * will cause the 3D init.
3203 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
3204 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
3206 IDirectDrawSurfaceImpl *target = object, *surface;
3209 /* Search for the primary to use as render target */
3210 LIST_FOR_EACH(entry, &This->surface_list)
3212 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3213 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
3214 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
3218 TRACE("Using primary %p as render target\n", target);
3223 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
3224 hr = ddraw_attach_d3d_device(This, target);
3227 IDirectDrawSurfaceImpl *release_surf;
3228 ERR("ddraw_attach_d3d_device failed, hr %#x\n", hr);
3231 /* The before created surface structures are in an incomplete state here.
3232 * WineD3D holds the reference on the IParents, and it released them on the failure
3233 * already. So the regular release method implementation would fail on the attempt
3234 * to destroy either the IParents or the swapchain. So free the surface here.
3235 * The surface structure here is a list, not a tree, because onscreen targets
3236 * cannot be cube textures
3240 release_surf = object;
3241 object = object->complex_array[0];
3242 ddraw_surface_destroy(release_surf);
3244 LeaveCriticalSection(&ddraw_cs);
3248 else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3250 ddraw_create_gdi_swapchain(This, object);
3253 /* Addref the ddraw interface to keep an reference for each surface */
3254 IDirectDraw7_AddRef(iface);
3255 object->ifaceToRelease = (IUnknown *) iface;
3257 /* Create a WineD3DTexture if a texture was requested */
3258 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3260 enum wined3d_format_id Format;
3262 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
3264 This->tex_root = object;
3266 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3268 /* a mipmap is created, create enough levels */
3269 levels = desc2.u2.dwMipMapCount;
3273 /* No mipmap is created, create one level */
3277 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
3278 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3280 Pool = WINED3DPOOL_SYSTEMMEM;
3282 /* Should I forward the MANAGED cap to the managed pool ? */
3284 /* Get the format. It's set already by CreateNewSurface */
3285 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
3287 /* The surfaces are already created, the callback only
3288 * passes the IWineD3DSurface to WineD3D
3290 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3292 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice, DDSD->dwWidth /* Edgelength */, levels,
3293 0 /* usage */, Format, Pool, object, &ddraw_null_wined3d_parent_ops,
3294 (IWineD3DCubeTexture **)&object->wineD3DTexture);
3298 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight, levels,
3299 0 /* usage */, Format, Pool, object, &ddraw_null_wined3d_parent_ops,
3300 (IWineD3DTexture **)&object->wineD3DTexture);
3302 This->tex_root = NULL;
3305 LeaveCriticalSection(&ddraw_cs);
3309 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3310 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3312 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
3313 IDirectDrawSurfaceImpl *impl;
3316 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3317 iface, surface_desc, surface, outer_unknown);
3319 hr = ddraw7_CreateSurface((IDirectDraw7 *)ddraw, surface_desc, (IDirectDrawSurface7 **)surface, outer_unknown);
3320 impl = (IDirectDrawSurfaceImpl *)*surface;
3321 if (SUCCEEDED(hr) && impl)
3323 ddraw_set_surface_version(impl, 4);
3324 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3325 IDirectDraw4_AddRef(iface);
3326 impl->ifaceToRelease = (IUnknown *)iface;
3332 static HRESULT WINAPI ddraw3_CreateSurface(IDirectDraw3 *iface,
3333 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3335 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
3336 IDirectDrawSurface7 *surface7;
3337 IDirectDrawSurfaceImpl *impl;
3340 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3341 iface, surface_desc, surface, outer_unknown);
3343 hr = ddraw7_CreateSurface((IDirectDraw7 *)ddraw, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3350 impl = (IDirectDrawSurfaceImpl *)surface7;
3351 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3352 ddraw_set_surface_version(impl, 3);
3353 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3354 IDirectDraw3_AddRef(iface);
3355 impl->ifaceToRelease = (IUnknown *)iface;
3360 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3361 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3363 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
3364 IDirectDrawSurface7 *surface7;
3365 IDirectDrawSurfaceImpl *impl;
3368 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3369 iface, surface_desc, surface, outer_unknown);
3371 hr = ddraw7_CreateSurface((IDirectDraw7 *)ddraw, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3378 impl = (IDirectDrawSurfaceImpl *)surface7;
3379 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3380 ddraw_set_surface_version(impl, 2);
3381 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3382 impl->ifaceToRelease = NULL;
3387 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3388 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3390 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
3391 IDirectDrawSurface7 *surface7;
3392 IDirectDrawSurfaceImpl *impl;
3395 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3396 iface, surface_desc, surface, outer_unknown);
3398 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3399 * primaries anyway. */
3400 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3401 hr = ddraw7_CreateSurface((IDirectDraw7 *)ddraw, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3408 impl = (IDirectDrawSurfaceImpl *)surface7;
3409 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3410 ddraw_set_surface_version(impl, 1);
3411 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3412 impl->ifaceToRelease = NULL;
3417 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3418 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3421 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3422 const DDPIXELFORMAT *provided)
3424 /* Some flags must be present in both or neither for a match. */
3425 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3426 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3427 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3429 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3432 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3435 if (requested->dwFlags & DDPF_FOURCC)
3436 if (requested->dwFourCC != provided->dwFourCC)
3439 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3440 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3441 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3444 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3445 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3446 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3449 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3450 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3453 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3454 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3456 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3459 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3460 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3466 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3475 #define CMP(FLAG, FIELD) \
3476 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3477 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3479 static const struct compare_info compare[] =
3481 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3482 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3484 CMP(CKDESTBLT, ddckCKDestBlt),
3485 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3486 CMP(CKSRCBLT, ddckCKSrcBlt),
3487 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3488 CMP(HEIGHT, dwHeight),
3489 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3490 CMP(LPSURFACE, lpSurface),
3491 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3492 CMP(PITCH, u1 /* lPitch */),
3493 /* PIXELFORMAT: manual */
3494 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3495 CMP(TEXTURESTAGE, dwTextureStage),
3496 CMP(WIDTH, dwWidth),
3497 /* ZBUFFERBITDEPTH: "obsolete" */
3504 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3507 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3509 if (requested->dwFlags & compare[i].flag
3510 && memcmp((const char *)provided + compare[i].offset,
3511 (const char *)requested + compare[i].offset,
3512 compare[i].size) != 0)
3516 if (requested->dwFlags & DDSD_PIXELFORMAT)
3518 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3519 &provided->u4.ddpfPixelFormat))
3526 #undef DDENUMSURFACES_SEARCHTYPE
3527 #undef DDENUMSURFACES_MATCHTYPE
3529 struct surfacescallback_context
3531 LPDDENUMSURFACESCALLBACK func;
3535 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3536 DDSURFACEDESC2 *surface_desc, void *context)
3538 struct surfacescallback_context *cbcontext = context;
3540 return cbcontext->func((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
3541 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3544 /*****************************************************************************
3545 * IDirectDraw7::EnumSurfaces
3547 * Loops through all surfaces attached to this device and calls the
3548 * application callback. This can't be relayed to WineD3DDevice,
3549 * because some WineD3DSurfaces' parents are IParent objects
3552 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3553 * DDSD: Description to filter for
3554 * Context: Application-provided pointer, it's passed unmodified to the
3556 * Callback: Address to call for each surface
3559 * DDERR_INVALIDPARAMS if the callback is NULL
3562 *****************************************************************************/
3563 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3564 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3566 /* The surface enumeration is handled by WineDDraw,
3567 * because it keeps track of all surfaces attached to
3568 * it. The filtering is done by our callback function,
3569 * because WineDDraw doesn't handle ddraw-like surface
3572 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3573 IDirectDrawSurfaceImpl *surf;
3575 DDSURFACEDESC2 desc;
3576 struct list *entry, *entry2;
3578 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3579 iface, Flags, DDSD, Context, Callback);
3581 all = Flags & DDENUMSURFACES_ALL;
3582 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3584 EnterCriticalSection(&ddraw_cs);
3588 LeaveCriticalSection(&ddraw_cs);
3589 return DDERR_INVALIDPARAMS;
3592 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3593 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3595 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3596 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3598 desc = surf->surface_desc;
3599 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
3600 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
3602 LeaveCriticalSection(&ddraw_cs);
3607 LeaveCriticalSection(&ddraw_cs);
3611 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3612 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3614 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3615 iface, flags, surface_desc, context, callback);
3617 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw4(iface),
3618 flags, surface_desc, context, (LPDDENUMSURFACESCALLBACK7)callback);
3621 static HRESULT WINAPI ddraw3_EnumSurfaces(IDirectDraw3 *iface, DWORD flags,
3622 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3624 struct surfacescallback_context cbcontext;
3626 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3627 iface, flags, surface_desc, context, callback);
3629 cbcontext.func = callback;
3630 cbcontext.context = context;
3632 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags,
3633 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumSurfacesCallbackThunk);
3636 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3637 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3639 struct surfacescallback_context cbcontext;
3641 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3642 iface, flags, surface_desc, context, callback);
3644 cbcontext.func = callback;
3645 cbcontext.context = context;
3647 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags,
3648 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumSurfacesCallbackThunk);
3651 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3652 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3654 struct surfacescallback_context cbcontext;
3656 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3657 iface, flags, surface_desc, context, callback);
3659 cbcontext.func = callback;
3660 cbcontext.context = context;
3662 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags,
3663 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumSurfacesCallbackThunk);
3666 /*****************************************************************************
3667 * DirectDrawCreateClipper (DDRAW.@)
3669 * Creates a new IDirectDrawClipper object.
3672 * Clipper: Address to write the interface pointer to
3673 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3677 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3678 * E_OUTOFMEMORY if allocating the object failed
3680 *****************************************************************************/
3682 DirectDrawCreateClipper(DWORD Flags,
3683 LPDIRECTDRAWCLIPPER *Clipper,
3686 IDirectDrawClipperImpl* object;
3689 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3690 Flags, Clipper, UnkOuter);
3692 EnterCriticalSection(&ddraw_cs);
3693 if (UnkOuter != NULL)
3695 LeaveCriticalSection(&ddraw_cs);
3696 return CLASS_E_NOAGGREGATION;
3701 LeaveCriticalSection(&ddraw_cs);
3702 return DDERR_NODIRECTDRAWSUPPORT;
3705 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3706 sizeof(IDirectDrawClipperImpl));
3709 LeaveCriticalSection(&ddraw_cs);
3710 return E_OUTOFMEMORY;
3713 hr = ddraw_clipper_init(object);
3716 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3717 HeapFree(GetProcessHeap(), 0, object);
3718 LeaveCriticalSection(&ddraw_cs);
3722 TRACE("Created clipper %p.\n", object);
3723 *Clipper = (IDirectDrawClipper *) object;
3724 LeaveCriticalSection(&ddraw_cs);
3728 /*****************************************************************************
3729 * IDirectDraw7::CreateClipper
3731 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3733 *****************************************************************************/
3734 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3735 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3737 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3738 iface, Flags, Clipper, UnkOuter);
3740 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3743 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface,
3744 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3746 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3747 iface, flags, clipper, outer_unknown);
3749 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw4(iface), flags, clipper, outer_unknown);
3752 static HRESULT WINAPI ddraw3_CreateClipper(IDirectDraw3 *iface,
3753 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3755 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3756 iface, flags, clipper, outer_unknown);
3758 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags, clipper, outer_unknown);
3761 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3762 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3764 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3765 iface, flags, clipper, outer_unknown);
3767 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags, clipper, outer_unknown);
3770 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3771 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3773 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3774 iface, flags, clipper, outer_unknown);
3776 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags, clipper, outer_unknown);
3779 /*****************************************************************************
3780 * IDirectDraw7::CreatePalette
3782 * Creates a new IDirectDrawPalette object
3785 * Flags: The flags for the new clipper
3786 * ColorTable: Color table to assign to the new clipper
3787 * Palette: Address to write the interface pointer to
3788 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3792 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3793 * E_OUTOFMEMORY if allocating the object failed
3795 *****************************************************************************/
3796 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3797 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3799 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3800 IDirectDrawPaletteImpl *object;
3803 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3804 iface, Flags, ColorTable, Palette, pUnkOuter);
3806 EnterCriticalSection(&ddraw_cs);
3807 if(pUnkOuter != NULL)
3809 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3810 LeaveCriticalSection(&ddraw_cs);
3811 return CLASS_E_NOAGGREGATION;
3814 /* The refcount test shows that a cooplevel is required for this */
3815 if(!This->cooperative_level)
3817 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3818 LeaveCriticalSection(&ddraw_cs);
3819 return DDERR_NOCOOPERATIVELEVELSET;
3822 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3825 ERR("Out of memory when allocating memory for a palette implementation\n");
3826 LeaveCriticalSection(&ddraw_cs);
3827 return E_OUTOFMEMORY;
3830 hr = ddraw_palette_init(object, This, Flags, ColorTable);
3833 WARN("Failed to initialize palette, hr %#x.\n", hr);
3834 HeapFree(GetProcessHeap(), 0, object);
3835 LeaveCriticalSection(&ddraw_cs);
3839 TRACE("Created palette %p.\n", object);
3840 *Palette = (IDirectDrawPalette *)object;
3841 LeaveCriticalSection(&ddraw_cs);
3845 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags,
3846 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3848 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
3851 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3852 iface, flags, entries, palette, outer_unknown);
3854 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3855 if (SUCCEEDED(hr) && *palette)
3857 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3858 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3859 IDirectDraw4_AddRef(iface);
3860 impl->ifaceToRelease = (IUnknown *)iface;
3865 static HRESULT WINAPI ddraw3_CreatePalette(IDirectDraw3 *iface, DWORD flags,
3866 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3868 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
3871 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3872 iface, flags, entries, palette, outer_unknown);
3874 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3875 if (SUCCEEDED(hr) && *palette)
3877 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3878 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3879 IDirectDraw4_AddRef(iface);
3880 impl->ifaceToRelease = (IUnknown *)iface;
3886 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3887 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3889 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
3892 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3893 iface, flags, entries, palette, outer_unknown);
3895 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3896 if (SUCCEEDED(hr) && *palette)
3898 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3899 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3900 impl->ifaceToRelease = NULL;
3906 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3907 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3909 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
3912 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3913 iface, flags, entries, palette, outer_unknown);
3915 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3916 if (SUCCEEDED(hr) && *palette)
3918 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3919 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3920 impl->ifaceToRelease = NULL;
3926 /*****************************************************************************
3927 * IDirectDraw7::DuplicateSurface
3929 * Duplicates a surface. The surface memory points to the same memory as
3930 * the original surface, and it's released when the last surface referencing
3931 * it is released. I guess that's beyond Wine's surface management right now
3932 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3933 * test application to implement this)
3936 * Src: Address of the source surface
3937 * Dest: Address to write the new surface pointer to
3940 * See IDirectDraw7::CreateSurface
3942 *****************************************************************************/
3943 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3944 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3946 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
3948 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3950 /* For now, simply create a new, independent surface */
3951 return IDirectDraw7_CreateSurface(iface,
3952 &Surf->surface_desc,
3957 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface,
3958 IDirectDrawSurface4 *src, IDirectDrawSurface4 **dst)
3960 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3962 return ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw4(iface),
3963 (IDirectDrawSurface7 *)src, (IDirectDrawSurface7 **)dst);
3966 static HRESULT WINAPI ddraw3_DuplicateSurface(IDirectDraw3 *iface,
3967 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3969 IDirectDrawSurface7 *src7, *dst7;
3972 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3973 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
3974 hr = ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw3(iface), src7, &dst7);
3977 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
3981 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3982 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3984 IDirectDrawSurface7 *src7, *dst7;
3987 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3988 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
3989 hr = ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw2(iface), src7, &dst7);
3992 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
3996 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface,
3997 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3999 IDirectDrawSurface7 *src7, *dst7;
4002 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4003 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4004 hr = ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw1(iface), src7, &dst7);
4007 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4011 /*****************************************************************************
4012 * IDirect3D7::EnumDevices
4014 * The EnumDevices method for IDirect3D7. It enumerates all supported
4015 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
4018 * callback: Function to call for each enumerated device
4019 * context: Pointer to pass back to the app
4022 * D3D_OK, or the return value of the GetCaps call
4024 *****************************************************************************/
4025 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
4027 char interface_name_tnl[] = "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D";
4028 char device_name_tnl[] = "Wine D3D7 T&L HAL";
4029 char interface_name_hal[] = "WINE Direct3D7 Hardware acceleration using WineD3D";
4030 char device_name_hal[] = "Wine D3D7 HAL";
4031 char interface_name_rgb[] = "WINE Direct3D7 RGB Software Emulation using WineD3D";
4032 char device_name_rgb[] = "Wine D3D7 RGB";
4034 IDirectDrawImpl *ddraw = ddraw_from_d3d7(iface);
4035 D3DDEVICEDESC7 device_desc7;
4036 D3DDEVICEDESC device_desc1;
4039 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4041 EnterCriticalSection(&ddraw_cs);
4043 hr = IDirect3DImpl_GetCaps(ddraw->wineD3D, &device_desc1, &device_desc7);
4046 LeaveCriticalSection(&ddraw_cs);
4049 callback(interface_name_tnl, device_name_tnl, &device_desc7, context);
4051 device_desc7.deviceGUID = IID_IDirect3DHALDevice;
4052 callback(interface_name_hal, device_name_hal, &device_desc7, context);
4054 device_desc7.deviceGUID = IID_IDirect3DRGBDevice;
4055 callback(interface_name_rgb, device_name_rgb, &device_desc7, context);
4057 TRACE("End of enumeration.\n");
4059 LeaveCriticalSection(&ddraw_cs);
4064 /*****************************************************************************
4065 * IDirect3D3::EnumDevices
4067 * Enumerates all supported Direct3DDevice interfaces. This is the
4068 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
4070 * Version 1, 2 and 3
4073 * callback: Application-provided routine to call for each enumerated device
4074 * Context: Pointer to pass to the callback
4077 * D3D_OK on success,
4078 * The result of IDirect3DImpl_GetCaps if it failed
4080 *****************************************************************************/
4081 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4083 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
4085 IDirectDrawImpl *ddraw = ddraw_from_d3d3(iface);
4086 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
4087 D3DDEVICEDESC7 device_desc7;
4090 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
4091 * name string. Let's put the string in a sufficiently sized array in
4092 * writable memory. */
4093 char device_name[50];
4094 strcpy(device_name,"Direct3D HEL");
4096 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4098 EnterCriticalSection(&ddraw_cs);
4100 hr = IDirect3DImpl_GetCaps(ddraw->wineD3D, &device_desc1, &device_desc7);
4103 LeaveCriticalSection(&ddraw_cs);
4107 /* Do I have to enumerate the reference id? Note from old d3d7:
4108 * "It seems that enumerating the reference IID on Direct3D 1 games
4109 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
4111 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
4112 * EnumReference which enables / disables enumerating the reference
4113 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
4114 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
4115 * demo directory suggest this.
4117 * Some games(GTA 2) seem to use the second enumerated device, so I have
4118 * to enumerate at least 2 devices. So enumerate the reference device to
4121 * Other games (Rollcage) tell emulation and hal device apart by certain
4122 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
4123 * limitation flag), and it refuses all devices that have the perspective
4124 * flag set. This way it refuses the emulation device, and HAL devices
4125 * never have POW2 unset in d3d7 on windows. */
4126 if (ddraw->d3dversion != 1)
4128 static CHAR reference_description[] = "RGB Direct3D emulation";
4130 TRACE("Enumerating WineD3D D3DDevice interface.\n");
4131 hal_desc = device_desc1;
4132 hel_desc = device_desc1;
4133 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
4134 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4135 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4136 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4137 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4138 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
4139 device_name, &hal_desc, &hel_desc, context);
4140 if (hr != D3DENUMRET_OK)
4142 TRACE("Application cancelled the enumeration.\n");
4143 LeaveCriticalSection(&ddraw_cs);
4148 strcpy(device_name,"Direct3D HAL");
4150 TRACE("Enumerating HAL Direct3D device.\n");
4151 hal_desc = device_desc1;
4152 hel_desc = device_desc1;
4153 /* The hal device does not have the pow2 flag set in hel, but in hal. */
4154 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4155 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4156 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4157 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4158 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
4159 device_name, &hal_desc, &hel_desc, context);
4160 if (hr != D3DENUMRET_OK)
4162 TRACE("Application cancelled the enumeration.\n");
4163 LeaveCriticalSection(&ddraw_cs);
4167 TRACE("End of enumeration.\n");
4169 LeaveCriticalSection(&ddraw_cs);
4173 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4175 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4177 return d3d3_EnumDevices((IDirect3D3 *)&ddraw_from_d3d2(iface)->IDirect3D3_vtbl, callback, context);
4180 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4182 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4184 return d3d3_EnumDevices((IDirect3D3 *)&ddraw_from_d3d1(iface)->IDirect3D3_vtbl, callback, context);
4187 /*****************************************************************************
4188 * IDirect3D3::CreateLight
4190 * Creates an IDirect3DLight interface. This interface is used in
4191 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4192 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4193 * uses the IDirect3DDevice7 interface with D3D7 lights.
4195 * Version 1, 2 and 3
4198 * light: Address to store the new interface pointer
4199 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4204 * DDERR_OUTOFMEMORY if memory allocation failed
4205 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4207 *****************************************************************************/
4208 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4210 IDirect3DLightImpl *object;
4212 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4214 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4216 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4219 ERR("Failed to allocate light memory.\n");
4220 return DDERR_OUTOFMEMORY;
4223 d3d_light_init(object, ddraw_from_d3d3(iface));
4225 TRACE("Created light %p.\n", object);
4226 *light = (IDirect3DLight *)object;
4231 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4233 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4235 return d3d3_CreateLight((IDirect3D3 *)&ddraw_from_d3d2(iface)->IDirect3D3_vtbl, light, outer_unknown);
4238 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4240 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4242 return d3d3_CreateLight((IDirect3D3 *)&ddraw_from_d3d1(iface)->IDirect3D3_vtbl, light, outer_unknown);
4245 /*****************************************************************************
4246 * IDirect3D3::CreateMaterial
4248 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4249 * and older versions. The IDirect3DMaterial implementation wraps its
4250 * functionality to IDirect3DDevice7::SetMaterial and friends.
4252 * Version 1, 2 and 3
4255 * material: Address to store the new interface's pointer to
4256 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4261 * DDERR_OUTOFMEMORY if memory allocation failed
4262 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4264 *****************************************************************************/
4265 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material, IUnknown *outer_unknown)
4267 IDirect3DMaterialImpl *object;
4269 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4271 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4273 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4276 ERR("Failed to allocate material memory.\n");
4277 return DDERR_OUTOFMEMORY;
4280 d3d_material_init(object, ddraw_from_d3d3(iface));
4282 TRACE("Created material %p.\n", object);
4283 *material = (IDirect3DMaterial3 *)object;
4288 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material, IUnknown *outer_unknown)
4290 IDirect3DMaterial3 *material3;
4293 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4295 hr = d3d3_CreateMaterial((IDirect3D3 *)&ddraw_from_d3d2(iface)->IDirect3D3_vtbl, &material3, outer_unknown);
4296 *material = material3 ? (IDirect3DMaterial2 *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial2_vtbl : NULL;
4298 TRACE("Returning material %p.\n", *material);
4303 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material, IUnknown *outer_unknown)
4305 IDirect3DMaterial3 *material3;
4308 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4310 hr = d3d3_CreateMaterial((IDirect3D3 *)&ddraw_from_d3d1(iface)->IDirect3D3_vtbl, &material3, outer_unknown);
4311 *material = material3 ? (IDirect3DMaterial *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial_vtbl : NULL;
4313 TRACE("Returning material %p.\n", *material);
4318 /*****************************************************************************
4319 * IDirect3D3::CreateViewport
4321 * Creates an IDirect3DViewport interface. This interface is used
4322 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4323 * it has been replaced by a viewport structure and
4324 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4325 * uses the IDirect3DDevice7 methods for its functionality
4328 * Viewport: Address to store the new interface pointer
4329 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4334 * DDERR_OUTOFMEMORY if memory allocation failed
4335 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4337 *****************************************************************************/
4338 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport, IUnknown *outer_unknown)
4340 IDirect3DViewportImpl *object;
4342 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4344 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4346 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4349 ERR("Failed to allocate viewport memory.\n");
4350 return DDERR_OUTOFMEMORY;
4353 d3d_viewport_init(object, ddraw_from_d3d3(iface));
4355 TRACE("Created viewport %p.\n", object);
4356 *viewport = (IDirect3DViewport3 *)object;
4361 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4363 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4365 return d3d3_CreateViewport((IDirect3D3 *)&ddraw_from_d3d2(iface)->IDirect3D3_vtbl,
4366 (IDirect3DViewport3 **)viewport, outer_unknown);
4369 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4371 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4373 return d3d3_CreateViewport((IDirect3D3 *)&ddraw_from_d3d1(iface)->IDirect3D3_vtbl,
4374 (IDirect3DViewport3 **)viewport, outer_unknown);
4377 /*****************************************************************************
4378 * IDirect3D3::FindDevice
4380 * This method finds a device with the requested properties and returns a
4381 * device description
4385 * fds: Describes the requested device characteristics
4386 * fdr: Returns the device description
4390 * DDERR_INVALIDPARAMS if no device was found
4392 *****************************************************************************/
4393 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4395 IDirectDrawImpl *ddraw = ddraw_from_d3d3(iface);
4396 D3DDEVICEDESC7 desc7;
4397 D3DDEVICEDESC desc1;
4400 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4402 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4404 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4405 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4406 return DDERR_INVALIDPARAMS;
4408 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4409 && fds->dcmColorModel != D3DCOLOR_RGB)
4411 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4412 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4415 if (fds->dwFlags & D3DFDS_GUID)
4417 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4418 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4419 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4420 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4422 WARN("No match for this GUID.\n");
4423 return DDERR_NOTFOUND;
4428 hr = IDirect3DImpl_GetCaps(ddraw->wineD3D, &desc1, &desc7);
4429 if (hr != D3D_OK) return hr;
4431 /* Now return our own GUID */
4432 fdr->guid = IID_D3DDEVICE_WineD3D;
4433 fdr->ddHwDesc = desc1;
4434 fdr->ddSwDesc = desc1;
4436 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4441 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4443 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4445 return d3d3_FindDevice((IDirect3D3 *)&ddraw_from_d3d2(iface)->IDirect3D3_vtbl, fds, fdr);
4448 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4450 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4452 return d3d3_FindDevice((IDirect3D3 *)&ddraw_from_d3d1(iface)->IDirect3D3_vtbl, fds, fdr);
4455 /*****************************************************************************
4456 * IDirect3D7::CreateDevice
4458 * Creates an IDirect3DDevice7 interface.
4460 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4461 * DirectDraw surfaces and are created with
4462 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4463 * create the device object and QueryInterfaces for IDirect3DDevice
4466 * refiid: IID of the device to create
4467 * Surface: Initial rendertarget
4468 * Device: Address to return the interface pointer
4472 * DDERR_OUTOFMEMORY if memory allocation failed
4473 * DDERR_INVALIDPARAMS if a device exists already
4475 *****************************************************************************/
4476 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4477 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4479 IDirectDrawSurfaceImpl *target = (IDirectDrawSurfaceImpl *)surface;
4480 IDirectDrawImpl *ddraw = ddraw_from_d3d7(iface);
4481 IDirect3DDeviceImpl *object;
4484 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4486 EnterCriticalSection(&ddraw_cs);
4489 /* Fail device creation if non-opengl surfaces are used. */
4490 if (ddraw->ImplType != SURFACE_OPENGL)
4492 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4493 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4495 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4496 * is caught in CreateSurface or QueryInterface. */
4497 LeaveCriticalSection(&ddraw_cs);
4501 if (ddraw->d3ddevice)
4503 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4504 LeaveCriticalSection(&ddraw_cs);
4505 return DDERR_INVALIDPARAMS;
4508 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4511 ERR("Failed to allocate device memory.\n");
4512 LeaveCriticalSection(&ddraw_cs);
4513 return DDERR_OUTOFMEMORY;
4516 hr = d3d_device_init(object, ddraw, target);
4519 WARN("Failed to initialize device, hr %#x.\n", hr);
4520 HeapFree(GetProcessHeap(), 0, object);
4521 LeaveCriticalSection(&ddraw_cs);
4525 TRACE("Created device %p.\n", object);
4526 *device = (IDirect3DDevice7 *)object;
4528 LeaveCriticalSection(&ddraw_cs);
4532 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4533 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4537 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4538 iface, debugstr_guid(riid), surface, device, outer_unknown);
4540 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4542 hr = d3d7_CreateDevice((IDirect3D7 *)&ddraw_from_d3d3(iface)->IDirect3D7_vtbl, riid,
4543 (IDirectDrawSurface7 *)surface, (IDirect3DDevice7 **)device);
4544 if (*device) *device = (IDirect3DDevice3 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice3_vtbl;
4549 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4550 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4554 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4555 iface, debugstr_guid(riid), surface, device);
4557 hr = d3d7_CreateDevice((IDirect3D7 *)&ddraw_from_d3d2(iface)->IDirect3D7_vtbl, riid,
4558 surface ? (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)surface) : NULL,
4559 (IDirect3DDevice7 **)device);
4560 if (*device) *device = (IDirect3DDevice2 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice2_vtbl;
4565 /*****************************************************************************
4566 * IDirect3D7::CreateVertexBuffer
4568 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4574 * desc: Requested Vertex buffer properties
4575 * vertex_buffer: Address to return the interface pointer at
4576 * flags: Some flags, should be 0
4580 * DDERR_OUTOFMEMORY if memory allocation failed
4581 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4582 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4584 *****************************************************************************/
4585 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4586 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4588 IDirect3DVertexBufferImpl *object;
4591 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4592 iface, desc, vertex_buffer, flags);
4594 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4596 TRACE("Vertex buffer description:\n");
4597 TRACE(" dwSize %u\n", desc->dwSize);
4598 TRACE(" dwCaps %#x\n", desc->dwCaps);
4599 TRACE(" FVF %#x\n", desc->dwFVF);
4600 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
4602 /* Now create the vertex buffer */
4603 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4606 ERR("Failed to allocate vertex buffer memory.\n");
4607 return DDERR_OUTOFMEMORY;
4610 hr = d3d_vertex_buffer_init(object, ddraw_from_d3d7(iface), desc);
4613 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
4614 HeapFree(GetProcessHeap(), 0, object);
4618 TRACE("Created vertex buffer %p.\n", object);
4619 *vertex_buffer = (IDirect3DVertexBuffer7 *)object;
4624 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4625 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4627 IDirectDrawImpl *This = ddraw_from_d3d3(iface);
4630 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4631 iface, desc, vertex_buffer, flags, outer_unknown);
4633 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4635 hr = d3d7_CreateVertexBuffer((IDirect3D7 *)&This->IDirect3D7_vtbl,
4636 desc, (IDirect3DVertexBuffer7 **)vertex_buffer, flags);
4638 *vertex_buffer = (IDirect3DVertexBuffer *)&((IDirect3DVertexBufferImpl *)*vertex_buffer)->IDirect3DVertexBuffer_vtbl;
4643 /*****************************************************************************
4644 * IDirect3D7::EnumZBufferFormats
4646 * Enumerates all supported Z buffer pixel formats
4652 * callback: callback to call for each pixel format
4653 * context: Pointer to pass back to the callback
4657 * DDERR_INVALIDPARAMS if callback is NULL
4658 * For details, see IWineD3DDevice::EnumZBufferFormats
4660 *****************************************************************************/
4661 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4662 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4664 IDirectDrawImpl *ddraw = ddraw_from_d3d7(iface);
4665 WINED3DDISPLAYMODE d3ddm;
4666 WINED3DDEVTYPE type;
4670 /* Order matters. Specifically, BattleZone II (full version) expects the
4671 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4672 static const enum wined3d_format_id formats[] =
4674 WINED3DFMT_S1_UINT_D15_UNORM,
4675 WINED3DFMT_D16_UNORM,
4676 WINED3DFMT_X8D24_UNORM,
4677 WINED3DFMT_S4X4_UINT_D24_UNORM,
4678 WINED3DFMT_D24_UNORM_S8_UINT,
4679 WINED3DFMT_D32_UNORM,
4682 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4683 iface, debugstr_guid(device_iid), callback, context);
4685 if (!callback) return DDERR_INVALIDPARAMS;
4687 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4688 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4689 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4691 TRACE("Asked for HAL device.\n");
4692 type = WINED3DDEVTYPE_HAL;
4694 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4695 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4697 TRACE("Asked for SW device.\n");
4698 type = WINED3DDEVTYPE_SW;
4700 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4702 TRACE("Asked for REF device.\n");
4703 type = WINED3DDEVTYPE_REF;
4705 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4707 TRACE("Asked for NULLREF device.\n");
4708 type = WINED3DDEVTYPE_NULLREF;
4712 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4713 type = WINED3DDEVTYPE_HAL;
4716 EnterCriticalSection(&ddraw_cs);
4717 /* We need an adapter format from somewhere to please wined3d and WGL.
4718 * Use the current display mode. So far all cards offer the same depth
4719 * stencil format for all modes, but if some do not and applications do
4720 * not like that we'll have to find some workaround, like iterating over
4721 * all imaginable formats and collecting all the depth stencil formats we
4723 hr = IWineD3DDevice_GetDisplayMode(ddraw->wineD3DDevice, 0, &d3ddm);
4725 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4727 hr = IWineD3D_CheckDeviceFormat(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
4728 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4731 DDPIXELFORMAT pformat;
4733 memset(&pformat, 0, sizeof(pformat));
4734 pformat.dwSize = sizeof(pformat);
4735 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4737 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4738 hr = callback(&pformat, context);
4739 if (hr != DDENUMRET_OK)
4741 TRACE("Format enumeration cancelled by application.\n");
4742 LeaveCriticalSection(&ddraw_cs);
4747 TRACE("End of enumeration.\n");
4749 LeaveCriticalSection(&ddraw_cs);
4753 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4754 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4756 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4757 iface, debugstr_guid(device_iid), callback, context);
4759 return d3d7_EnumZBufferFormats((IDirect3D7 *)&ddraw_from_d3d3(iface)->IDirect3D7_vtbl,
4760 device_iid, callback, context);
4763 /*****************************************************************************
4764 * IDirect3D7::EvictManagedTextures
4766 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4767 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4772 * D3D_OK, because it's a stub
4774 *****************************************************************************/
4775 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4777 FIXME("iface %p stub!\n", iface);
4779 /* TODO: Just enumerate resources using IWineD3DDevice_EnumResources(),
4780 * then unload surfaces / textures. */
4785 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4787 TRACE("iface %p.\n", iface);
4789 return d3d7_EvictManagedTextures((IDirect3D7 *)&ddraw_from_d3d3(iface)->IDirect3D7_vtbl);
4792 /*****************************************************************************
4793 * IDirect3DImpl_GetCaps
4795 * This function retrieves the device caps from wined3d
4796 * and converts it into a D3D7 and D3D - D3D3 structure
4797 * This is a helper function called from various places in ddraw
4800 * wined3d: The interface to get the caps from
4801 * desc1: Old D3D <3 structure to fill (needed)
4802 * desc7: D3D7 device desc structure to fill (needed)
4805 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4807 *****************************************************************************/
4808 HRESULT IDirect3DImpl_GetCaps(IWineD3D *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4810 WINED3DCAPS wined3d_caps;
4813 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4815 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4817 EnterCriticalSection(&ddraw_cs);
4818 hr = IWineD3D_GetDeviceCaps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
4819 LeaveCriticalSection(&ddraw_cs);
4822 WARN("Failed to get device caps, hr %#x.\n", hr);
4826 /* Copy the results into the d3d7 and d3d3 structures */
4827 desc7->dwDevCaps = wined3d_caps.DevCaps;
4828 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4829 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4830 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4831 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4832 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4833 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4834 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4835 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4836 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4837 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4839 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4840 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4842 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4843 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4844 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4845 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4847 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4848 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4849 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4850 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4852 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4853 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4855 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4856 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4858 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4859 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4861 /* Remove all non-d3d7 caps */
4862 desc7->dwDevCaps &= (
4863 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4864 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4865 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4866 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4867 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4868 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4869 D3DDEVCAPS_HWRASTERIZATION);
4871 desc7->dwStencilCaps &= (
4872 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4873 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4874 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4878 desc7->dwTextureOpCaps &= (
4879 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4880 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4881 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4882 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4883 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4884 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4885 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4886 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4888 desc7->dwVertexProcessingCaps &= (
4889 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4890 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4892 desc7->dpcLineCaps.dwMiscCaps &= (
4893 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4894 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4895 D3DPMISCCAPS_CULLCCW);
4897 desc7->dpcLineCaps.dwRasterCaps &= (
4898 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4899 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4900 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4901 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4902 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4903 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4904 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4905 D3DPRASTERCAPS_ZFOG);
4907 desc7->dpcLineCaps.dwZCmpCaps &= (
4908 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4909 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4910 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4912 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4913 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4914 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4915 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4916 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4917 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4919 desc7->dpcLineCaps.dwDestBlendCaps &= (
4920 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4921 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4922 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4923 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4924 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4926 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4927 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4928 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4929 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4931 desc7->dpcLineCaps.dwShadeCaps &= (
4932 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4933 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4934 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4935 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4936 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4937 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4938 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4940 desc7->dpcLineCaps.dwTextureCaps &= (
4941 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4942 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4943 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4944 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4946 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4947 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4948 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4949 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4950 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4951 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4952 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4954 desc7->dpcLineCaps.dwTextureBlendCaps &= (
4955 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
4956 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
4957 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
4959 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4960 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4961 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4963 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4965 /* DirectX7 always has the np2 flag set, no matter what the card
4966 * supports. Some old games (Rollcage) check the caps incorrectly.
4967 * If wined3d supports nonpow2 textures it also has np2 conditional
4969 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4972 /* Fill the missing members, and do some fixup */
4973 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4974 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4975 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4976 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4977 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4978 desc7->dpcLineCaps.dwStippleWidth = 32;
4979 desc7->dpcLineCaps.dwStippleHeight = 32;
4980 /* Use the same for the TriCaps */
4981 desc7->dpcTriCaps = desc7->dpcLineCaps;
4983 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4984 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4985 desc7->dwMinTextureWidth = 1;
4986 desc7->dwMinTextureHeight = 1;
4988 /* Convert DWORDs safely to WORDs */
4989 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4990 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4991 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4992 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4994 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4995 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4996 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4997 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4999 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
5001 desc7->dwReserved1 = 0;
5002 desc7->dwReserved2 = 0;
5003 desc7->dwReserved3 = 0;
5004 desc7->dwReserved4 = 0;
5006 /* Fill the old structure */
5007 memset(desc1, 0, sizeof(desc1));
5008 desc1->dwSize = sizeof(D3DDEVICEDESC);
5009 desc1->dwFlags = D3DDD_COLORMODEL
5011 | D3DDD_TRANSFORMCAPS
5013 | D3DDD_LIGHTINGCAPS
5016 | D3DDD_DEVICERENDERBITDEPTH
5017 | D3DDD_DEVICEZBUFFERBITDEPTH
5018 | D3DDD_MAXBUFFERSIZE
5019 | D3DDD_MAXVERTEXCOUNT;
5021 desc1->dcmColorModel = D3DCOLOR_RGB;
5022 desc1->dwDevCaps = desc7->dwDevCaps;
5023 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
5024 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
5025 desc1->bClipping = TRUE;
5026 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
5027 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
5028 | D3DLIGHTCAPS_PARALLELPOINT
5029 | D3DLIGHTCAPS_POINT
5030 | D3DLIGHTCAPS_SPOT;
5032 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
5033 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
5035 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
5036 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
5037 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
5038 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
5039 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
5040 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
5041 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
5042 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
5043 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
5044 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
5045 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
5046 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
5047 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
5049 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
5050 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
5051 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
5052 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
5053 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
5054 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
5055 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
5056 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
5057 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
5058 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
5059 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
5060 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
5061 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
5063 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
5064 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
5065 desc1->dwMaxBufferSize = 0;
5066 desc1->dwMaxVertexCount = 65536;
5067 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
5068 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
5069 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
5070 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
5071 desc1->dwMinStippleWidth = 1;
5072 desc1->dwMinStippleHeight = 1;
5073 desc1->dwMaxStippleWidth = 32;
5074 desc1->dwMaxStippleHeight = 32;
5075 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
5076 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
5077 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
5078 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
5079 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
5080 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
5081 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
5082 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
5083 desc1->dwStencilCaps = desc7->dwStencilCaps;
5084 desc1->dwFVFCaps = desc7->dwFVFCaps;
5085 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
5086 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
5087 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
5092 /*****************************************************************************
5093 * IDirectDraw7 VTable
5094 *****************************************************************************/
5095 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
5098 ddraw7_QueryInterface,
5103 ddraw7_CreateClipper,
5104 ddraw7_CreatePalette,
5105 ddraw7_CreateSurface,
5106 ddraw7_DuplicateSurface,
5107 ddraw7_EnumDisplayModes,
5108 ddraw7_EnumSurfaces,
5109 ddraw7_FlipToGDISurface,
5111 ddraw7_GetDisplayMode,
5112 ddraw7_GetFourCCCodes,
5113 ddraw7_GetGDISurface,
5114 ddraw7_GetMonitorFrequency,
5116 ddraw7_GetVerticalBlankStatus,
5118 ddraw7_RestoreDisplayMode,
5119 ddraw7_SetCooperativeLevel,
5120 ddraw7_SetDisplayMode,
5121 ddraw7_WaitForVerticalBlank,
5123 ddraw7_GetAvailableVidMem,
5125 ddraw7_GetSurfaceFromDC,
5127 ddraw7_RestoreAllSurfaces,
5128 ddraw7_TestCooperativeLevel,
5129 ddraw7_GetDeviceIdentifier,
5131 ddraw7_StartModeTest,
5135 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5138 ddraw4_QueryInterface,
5143 ddraw4_CreateClipper,
5144 ddraw4_CreatePalette,
5145 ddraw4_CreateSurface,
5146 ddraw4_DuplicateSurface,
5147 ddraw4_EnumDisplayModes,
5148 ddraw4_EnumSurfaces,
5149 ddraw4_FlipToGDISurface,
5151 ddraw4_GetDisplayMode,
5152 ddraw4_GetFourCCCodes,
5153 ddraw4_GetGDISurface,
5154 ddraw4_GetMonitorFrequency,
5156 ddraw4_GetVerticalBlankStatus,
5158 ddraw4_RestoreDisplayMode,
5159 ddraw4_SetCooperativeLevel,
5160 ddraw4_SetDisplayMode,
5161 ddraw4_WaitForVerticalBlank,
5163 ddraw4_GetAvailableVidMem,
5165 ddraw4_GetSurfaceFromDC,
5167 ddraw4_RestoreAllSurfaces,
5168 ddraw4_TestCooperativeLevel,
5169 ddraw4_GetDeviceIdentifier,
5172 static const struct IDirectDraw3Vtbl ddraw3_vtbl =
5175 ddraw3_QueryInterface,
5180 ddraw3_CreateClipper,
5181 ddraw3_CreatePalette,
5182 ddraw3_CreateSurface,
5183 ddraw3_DuplicateSurface,
5184 ddraw3_EnumDisplayModes,
5185 ddraw3_EnumSurfaces,
5186 ddraw3_FlipToGDISurface,
5188 ddraw3_GetDisplayMode,
5189 ddraw3_GetFourCCCodes,
5190 ddraw3_GetGDISurface,
5191 ddraw3_GetMonitorFrequency,
5193 ddraw3_GetVerticalBlankStatus,
5195 ddraw3_RestoreDisplayMode,
5196 ddraw3_SetCooperativeLevel,
5197 ddraw3_SetDisplayMode,
5198 ddraw3_WaitForVerticalBlank,
5200 ddraw3_GetAvailableVidMem,
5202 ddraw3_GetSurfaceFromDC,
5205 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5208 ddraw2_QueryInterface,
5213 ddraw2_CreateClipper,
5214 ddraw2_CreatePalette,
5215 ddraw2_CreateSurface,
5216 ddraw2_DuplicateSurface,
5217 ddraw2_EnumDisplayModes,
5218 ddraw2_EnumSurfaces,
5219 ddraw2_FlipToGDISurface,
5221 ddraw2_GetDisplayMode,
5222 ddraw2_GetFourCCCodes,
5223 ddraw2_GetGDISurface,
5224 ddraw2_GetMonitorFrequency,
5226 ddraw2_GetVerticalBlankStatus,
5228 ddraw2_RestoreDisplayMode,
5229 ddraw2_SetCooperativeLevel,
5230 ddraw2_SetDisplayMode,
5231 ddraw2_WaitForVerticalBlank,
5233 ddraw2_GetAvailableVidMem,
5236 static const struct IDirectDrawVtbl ddraw1_vtbl =
5239 ddraw1_QueryInterface,
5244 ddraw1_CreateClipper,
5245 ddraw1_CreatePalette,
5246 ddraw1_CreateSurface,
5247 ddraw1_DuplicateSurface,
5248 ddraw1_EnumDisplayModes,
5249 ddraw1_EnumSurfaces,
5250 ddraw1_FlipToGDISurface,
5252 ddraw1_GetDisplayMode,
5253 ddraw1_GetFourCCCodes,
5254 ddraw1_GetGDISurface,
5255 ddraw1_GetMonitorFrequency,
5257 ddraw1_GetVerticalBlankStatus,
5259 ddraw1_RestoreDisplayMode,
5260 ddraw1_SetCooperativeLevel,
5261 ddraw1_SetDisplayMode,
5262 ddraw1_WaitForVerticalBlank,
5265 static const struct IDirect3D7Vtbl d3d7_vtbl =
5267 /* IUnknown methods */
5268 d3d7_QueryInterface,
5271 /* IDirect3D7 methods */
5274 d3d7_CreateVertexBuffer,
5275 d3d7_EnumZBufferFormats,
5276 d3d7_EvictManagedTextures
5279 static const struct IDirect3D3Vtbl d3d3_vtbl =
5281 /* IUnknown methods */
5282 d3d3_QueryInterface,
5285 /* IDirect3D3 methods */
5288 d3d3_CreateMaterial,
5289 d3d3_CreateViewport,
5292 d3d3_CreateVertexBuffer,
5293 d3d3_EnumZBufferFormats,
5294 d3d3_EvictManagedTextures
5297 static const struct IDirect3D2Vtbl d3d2_vtbl =
5299 /* IUnknown methods */
5300 d3d2_QueryInterface,
5303 /* IDirect3D2 methods */
5306 d3d2_CreateMaterial,
5307 d3d2_CreateViewport,
5312 static const struct IDirect3DVtbl d3d1_vtbl =
5314 /* IUnknown methods */
5315 d3d1_QueryInterface,
5318 /* IDirect3D methods */
5322 d3d1_CreateMaterial,
5323 d3d1_CreateViewport,
5327 /*****************************************************************************
5330 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5331 * if none was found.
5333 * This function is in ddraw.c and the DDraw object space because D3D7
5334 * vertex buffers are created using the IDirect3D interface to the ddraw
5335 * object, so they can be valid across D3D devices(theoretically. The ddraw
5336 * object also owns the wined3d device
5340 * fvf: Fvf to find the decl for
5343 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
5346 *****************************************************************************/
5347 IWineD3DVertexDeclaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5350 IWineD3DVertexDeclaration* pDecl = NULL;
5351 int p, low, high; /* deliberately signed */
5352 struct FvfToDecl *convertedDecls = This->decls;
5354 TRACE("Searching for declaration for fvf %08x... ", fvf);
5357 high = This->numConvertedDecls - 1;
5358 while(low <= high) {
5359 p = (low + high) >> 1;
5361 if(convertedDecls[p].fvf == fvf) {
5362 TRACE("found %p\n", convertedDecls[p].decl);
5363 return convertedDecls[p].decl;
5364 } else if(convertedDecls[p].fvf < fvf) {
5370 TRACE("not found. Creating and inserting at position %d.\n", low);
5372 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
5373 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5374 if (hr != S_OK) return NULL;
5376 if(This->declArraySize == This->numConvertedDecls) {
5377 int grow = max(This->declArraySize / 2, 8);
5378 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5379 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5380 if(!convertedDecls) {
5381 /* This will destroy it */
5382 IWineD3DVertexDeclaration_Release(pDecl);
5385 This->decls = convertedDecls;
5386 This->declArraySize += grow;
5389 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5390 convertedDecls[low].decl = pDecl;
5391 convertedDecls[low].fvf = fvf;
5392 This->numConvertedDecls++;
5394 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5398 /* IWineD3DDeviceParent IUnknown methods */
5400 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
5402 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
5405 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
5407 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5408 return ddraw7_QueryInterface((IDirectDraw7 *)This, riid, object);
5411 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
5413 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5414 return ddraw7_AddRef((IDirectDraw7 *)This);
5417 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
5419 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5420 return ddraw7_Release((IDirectDraw7 *)This);
5423 /* IWineD3DDeviceParent methods */
5425 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
5427 TRACE("iface %p, device %p\n", iface, device);
5430 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
5431 IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5432 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
5434 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5435 IDirectDrawSurfaceImpl *surf = NULL;
5437 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
5439 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
5440 "\tpool %#x, level %u, face %u, surface %p\n",
5441 iface, superior, width, height, format, usage, pool, level, face, surface);
5443 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5446 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5447 TRACE("Asked for positive x\n");
5448 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5450 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5452 surf = This->tex_root; break;
5453 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5454 TRACE("Asked for negative x\n");
5455 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5456 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5457 TRACE("Asked for positive y\n");
5458 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5459 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5460 TRACE("Asked for negative y\n");
5461 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5462 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5463 TRACE("Asked for positive z\n");
5464 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5465 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5466 TRACE("Asked for negative z\n");
5467 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5468 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5473 IDirectDrawSurface7 *attached;
5474 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
5475 surf = (IDirectDrawSurfaceImpl *)attached;
5476 IDirectDrawSurface7_Release(attached);
5478 if (!surf) ERR("root search surface not found\n");
5480 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5483 IDirectDrawSurface7 *attached;
5484 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
5485 if(!attached) ERR("Surface not found\n");
5486 surf = (IDirectDrawSurfaceImpl *)attached;
5487 IDirectDrawSurface7_Release(attached);
5491 /* Return the surface */
5492 *surface = surf->WineD3DSurface;
5493 IWineD3DSurface_AddRef(*surface);
5495 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5500 static HRESULT WINAPI findRenderTarget(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *ctx)
5502 IDirectDrawSurfaceImpl *s = (IDirectDrawSurfaceImpl *)surface;
5503 IDirectDrawSurfaceImpl **target = ctx;
5505 if (!s->isRenderTarget)
5508 IDirectDrawSurface7_Release(surface);
5509 return DDENUMRET_CANCEL;
5512 /* Recurse into the surface tree */
5513 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
5515 IDirectDrawSurface7_Release(surface);
5516 if (*target) return DDENUMRET_CANCEL;
5518 return DDENUMRET_OK;
5521 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
5522 IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format,
5523 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5524 IWineD3DSurface **surface)
5526 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5527 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
5528 IDirectDrawSurfaceImpl *target = NULL;
5530 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5531 "\tmultisample_quality %u, lockable %u, surface %p\n",
5532 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
5534 if (d3d_surface->isRenderTarget)
5536 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
5540 target = d3d_surface;
5545 target = This->d3d_target;
5546 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
5549 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
5551 *surface = target->WineD3DSurface;
5552 IWineD3DSurface_AddRef(*surface);
5553 target->isRenderTarget = TRUE;
5555 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
5560 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
5561 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5562 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
5564 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5565 IDirectDrawSurfaceImpl *ddraw_surface;
5566 DDSURFACEDESC2 ddsd;
5569 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5570 "\tmultisample_quality %u, discard %u, surface %p\n",
5571 iface, width, height, format, multisample_type, multisample_quality, discard, surface);
5575 /* Create a DirectDraw surface */
5576 memset(&ddsd, 0, sizeof(ddsd));
5577 ddsd.dwSize = sizeof(ddsd);
5578 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
5579 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5580 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5581 ddsd.dwHeight = height;
5582 ddsd.dwWidth = width;
5585 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
5589 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
5592 This->depthstencil = TRUE;
5593 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
5594 This->depthstencil = FALSE;
5597 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
5601 *surface = ddraw_surface->WineD3DSurface;
5602 IWineD3DSurface_AddRef(*surface);
5603 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
5608 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
5609 IUnknown *superior, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5610 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
5612 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
5613 iface, superior, width, height, depth, format, pool, usage, volume);
5615 ERR("Not implemented!\n");
5620 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
5621 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
5623 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5624 IDirectDrawSurfaceImpl *iterator;
5625 IParentImpl *object;
5628 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
5630 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
5633 FIXME("Allocation of memory failed\n");
5635 return DDERR_OUTOFVIDEOMEMORY;
5638 ddraw_parent_init(object);
5640 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
5641 This->ImplType, object, swapchain);
5644 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
5645 HeapFree(GetProcessHeap(), 0 , object);
5650 object->child = (IUnknown *)*swapchain;
5651 This->d3d_target->wineD3DSwapChain = *swapchain;
5652 iterator = This->d3d_target->complex_array[0];
5655 iterator->wineD3DSwapChain = *swapchain;
5656 iterator = iterator->complex_array[0];
5662 static const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
5664 /* IUnknown methods */
5665 device_parent_QueryInterface,
5666 device_parent_AddRef,
5667 device_parent_Release,
5668 /* IWineD3DDeviceParent methods */
5669 device_parent_WineD3DDeviceCreated,
5670 device_parent_CreateSurface,
5671 device_parent_CreateRenderTarget,
5672 device_parent_CreateDepthStencilSurface,
5673 device_parent_CreateVolume,
5674 device_parent_CreateSwapChain,
5677 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5682 ddraw->lpVtbl = &ddraw7_vtbl;
5683 ddraw->IDirectDraw_vtbl = &ddraw1_vtbl;
5684 ddraw->IDirectDraw2_vtbl = &ddraw2_vtbl;
5685 ddraw->IDirectDraw3_vtbl = &ddraw3_vtbl;
5686 ddraw->IDirectDraw4_vtbl = &ddraw4_vtbl;
5687 ddraw->IDirect3D_vtbl = &d3d1_vtbl;
5688 ddraw->IDirect3D2_vtbl = &d3d2_vtbl;
5689 ddraw->IDirect3D3_vtbl = &d3d3_vtbl;
5690 ddraw->IDirect3D7_vtbl = &d3d7_vtbl;
5691 ddraw->device_parent_vtbl = &ddraw_wined3d_device_parent_vtbl;
5692 ddraw->numIfaces = 1;
5695 /* See comments in IDirectDrawImpl_CreateNewSurface for a description of
5697 ddraw->ImplType = DefaultSurfaceType;
5699 /* Get the current screen settings. */
5701 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5703 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5704 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5708 ERR("Failed to load wined3d - broken OpenGL setup?\n");
5709 return DDERR_NODIRECTDRAWSUPPORT;
5712 ddraw->wineD3D = pWineDirect3DCreate(7, (IUnknown *)ddraw);
5713 if (!ddraw->wineD3D)
5715 WARN("Failed to create a wined3d object.\n");
5716 return E_OUTOFMEMORY;
5719 hr = IWineD3D_CreateDevice(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type, NULL, 0,
5720 (IWineD3DDeviceParent *)&ddraw->device_parent_vtbl, &ddraw->wineD3DDevice);
5723 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5724 IWineD3D_Release(ddraw->wineD3D);
5728 /* Get the amount of video memory */
5729 ddraw->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(ddraw->wineD3DDevice);
5731 list_init(&ddraw->surface_list);