mscoree/tests: Mark a weird w2k3 result as broken.
[wine/multimedia.git] / dlls / ddraw / ddraw.c
blob51e8c8c4ddc93f4ab5e20591cad3a762d8d1a255
1 /*
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
23 #include "config.h"
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 =
33 "display",
34 "DirectDraw HAL",
35 { { 0x00010001, 0x00010001 } },
36 0, 0, 0, 0,
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static struct enum_device_entry
44 char interface_name[100];
45 char device_name[100];
46 const GUID *device_guid;
47 } device_list7[] =
49 /* T&L HAL device */
51 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
52 "Wine D3D7 T&L HAL",
53 &IID_IDirect3DTnLHalDevice,
56 /* HAL device */
58 "WINE Direct3D7 Hardware acceleration using WineD3D",
59 "Wine D3D7 HAL",
60 &IID_IDirect3DHALDevice,
63 /* RGB device */
65 "WINE Direct3D7 RGB Software Emulation using WineD3D",
66 "Wine D3D7 RGB",
67 &IID_IDirect3DRGBDevice,
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
75 ddraw_null_wined3d_object_destroyed,
78 static inline IDirectDrawImpl *impl_from_IDirectDraw(IDirectDraw *iface)
80 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw_iface);
83 static inline IDirectDrawImpl *impl_from_IDirectDraw2(IDirectDraw2 *iface)
85 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw2_iface);
88 static inline IDirectDrawImpl *impl_from_IDirectDraw3(IDirectDraw3 *iface)
90 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw3_iface);
93 static inline IDirectDrawImpl *impl_from_IDirectDraw4(IDirectDraw4 *iface)
95 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw4_iface);
98 static inline IDirectDrawImpl *impl_from_IDirectDraw7(IDirectDraw7 *iface)
100 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw7_iface);
103 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
105 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
108 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
110 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
113 static inline IDirectDrawImpl *impl_from_IDirect3D3(IDirect3D3 *iface)
115 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D3_iface);
118 static inline IDirectDrawImpl *impl_from_IDirect3D7(IDirect3D7 *iface)
120 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D7_iface);
123 /*****************************************************************************
124 * IUnknown Methods
125 *****************************************************************************/
127 /*****************************************************************************
128 * IDirectDraw7::QueryInterface
130 * Queries different interfaces of the DirectDraw object. It can return
131 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
132 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
133 * method.
134 * The returned interface is AddRef()-ed before it's returned
136 * Used for version 1, 2, 4 and 7
138 * Params:
139 * refiid: Interface ID asked for
140 * obj: Used to return the interface pointer
142 * Returns:
143 * S_OK if an interface was found
144 * E_NOINTERFACE if the requested interface wasn't found
146 *****************************************************************************/
147 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
149 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
151 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
153 /* Can change surface impl type */
154 EnterCriticalSection(&ddraw_cs);
156 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
157 *obj = NULL;
159 if(!refiid)
161 LeaveCriticalSection(&ddraw_cs);
162 return DDERR_INVALIDPARAMS;
165 /* Check DirectDraw Interfaces */
166 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
167 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
169 *obj = This;
170 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
172 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
174 *obj = &This->IDirectDraw4_iface;
175 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
177 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
179 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
180 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
181 *obj = NULL;
182 LeaveCriticalSection(&ddraw_cs);
183 return E_NOINTERFACE;
185 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
187 *obj = &This->IDirectDraw2_iface;
188 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
190 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
192 *obj = &This->IDirectDraw_iface;
193 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
196 /* Direct3D
197 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
198 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
199 * who had this idea and why. The older interfaces can query and IDirect3D version
200 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
201 * and messy to implement with the common creation function, so it has been left out here.
203 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
204 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
205 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
206 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
208 /* Check the surface implementation */
209 if(This->ImplType == SURFACE_UNKNOWN)
211 /* Apps may create the IDirect3D Interface before the primary surface.
212 * set the surface implementation */
213 This->ImplType = SURFACE_OPENGL;
214 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
216 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
218 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
219 ERR(" (%p) You may want to contact wine-devel for help\n", This);
220 /* Should I assert(0) here??? */
222 else if(This->ImplType != SURFACE_OPENGL)
224 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
225 /* Do not abort here, only reject 3D Device creation */
228 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
230 This->d3dversion = 1;
231 *obj = &This->IDirect3D_iface;
232 TRACE(" returning Direct3D interface at %p.\n", *obj);
234 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
236 This->d3dversion = 2;
237 *obj = &This->IDirect3D2_iface;
238 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
240 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
242 This->d3dversion = 3;
243 *obj = &This->IDirect3D3_iface;
244 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
246 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
248 This->d3dversion = 7;
249 *obj = &This->IDirect3D7_iface;
250 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
253 /* Unknown interface */
254 else
256 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
257 LeaveCriticalSection(&ddraw_cs);
258 return E_NOINTERFACE;
261 IUnknown_AddRef( (IUnknown *) *obj );
262 LeaveCriticalSection(&ddraw_cs);
263 return S_OK;
266 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
268 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
270 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
272 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
275 static HRESULT WINAPI ddraw3_QueryInterface(IDirectDraw3 *iface, REFIID riid, void **object)
277 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
279 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
281 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
284 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
286 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
288 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
290 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
293 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
295 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
297 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
299 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
302 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
304 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
306 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
311 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
313 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
315 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
317 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
320 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
322 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
324 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
326 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
329 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
331 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
333 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
335 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
338 /*****************************************************************************
339 * IDirectDraw7::AddRef
341 * Increases the interfaces refcount, basically
343 * DDraw refcounting is a bit tricky. The different DirectDraw interface
344 * versions have individual refcounts, but the IDirect3D interfaces do not.
345 * All interfaces are from one object, that means calling QueryInterface on an
346 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
347 * IDirectDrawImpl object.
349 * That means all AddRef and Release implementations of IDirectDrawX work
350 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
351 * except of IDirect3D7 which thunks to IDirectDraw7
353 * Returns: The new refcount
355 *****************************************************************************/
356 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
358 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
359 ULONG ref = InterlockedIncrement(&This->ref7);
361 TRACE("%p increasing refcount to %u.\n", This, ref);
363 if(ref == 1) InterlockedIncrement(&This->numIfaces);
365 return ref;
368 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
370 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
371 ULONG ref = InterlockedIncrement(&This->ref4);
373 TRACE("%p increasing refcount to %u.\n", This, ref);
375 if (ref == 1) InterlockedIncrement(&This->numIfaces);
377 return ref;
380 static ULONG WINAPI ddraw3_AddRef(IDirectDraw3 *iface)
382 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
383 ULONG ref = InterlockedIncrement(&This->ref3);
385 TRACE("%p increasing refcount to %u.\n", This, ref);
387 if (ref == 1) InterlockedIncrement(&This->numIfaces);
389 return ref;
392 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
394 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
395 ULONG ref = InterlockedIncrement(&This->ref2);
397 TRACE("%p increasing refcount to %u.\n", This, ref);
399 if (ref == 1) InterlockedIncrement(&This->numIfaces);
401 return ref;
404 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
406 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
407 ULONG ref = InterlockedIncrement(&This->ref1);
409 TRACE("%p increasing refcount to %u.\n", This, ref);
411 if (ref == 1) InterlockedIncrement(&This->numIfaces);
413 return ref;
416 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
418 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
420 TRACE("iface %p.\n", iface);
422 return ddraw7_AddRef(&This->IDirectDraw7_iface);
425 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
427 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
429 TRACE("iface %p.\n", iface);
431 return ddraw1_AddRef(&This->IDirectDraw_iface);
434 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
436 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
438 TRACE("iface %p.\n", iface);
440 return ddraw1_AddRef(&This->IDirectDraw_iface);
443 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
445 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
447 TRACE("iface %p.\n", iface);
449 return ddraw1_AddRef(&This->IDirectDraw_iface);
452 /*****************************************************************************
453 * ddraw_destroy
455 * Destroys a ddraw object if all refcounts are 0. This is to share code
456 * between the IDirectDrawX::Release functions
458 * Params:
459 * This: DirectDraw object to destroy
461 *****************************************************************************/
462 static void ddraw_destroy(IDirectDrawImpl *This)
464 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
465 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
467 /* Destroy the device window if we created one */
468 if(This->devicewindow != 0)
470 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
471 DestroyWindow(This->devicewindow);
472 This->devicewindow = 0;
475 EnterCriticalSection(&ddraw_cs);
476 list_remove(&This->ddraw_list_entry);
477 LeaveCriticalSection(&ddraw_cs);
479 /* Release the attached WineD3D stuff */
480 wined3d_device_decref(This->wined3d_device);
481 wined3d_decref(This->wineD3D);
483 /* Now free the object */
484 HeapFree(GetProcessHeap(), 0, This);
487 /*****************************************************************************
488 * IDirectDraw7::Release
490 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
492 * Returns: The new refcount
493 *****************************************************************************/
494 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
496 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
497 ULONG ref = InterlockedDecrement(&This->ref7);
499 TRACE("%p decreasing refcount to %u.\n", This, ref);
501 if (!ref && !InterlockedDecrement(&This->numIfaces))
502 ddraw_destroy(This);
504 return ref;
507 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
509 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
510 ULONG ref = InterlockedDecrement(&This->ref4);
512 TRACE("%p decreasing refcount to %u.\n", This, ref);
514 if (!ref && !InterlockedDecrement(&This->numIfaces))
515 ddraw_destroy(This);
517 return ref;
520 static ULONG WINAPI ddraw3_Release(IDirectDraw3 *iface)
522 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
523 ULONG ref = InterlockedDecrement(&This->ref3);
525 TRACE("%p decreasing refcount to %u.\n", This, ref);
527 if (!ref && !InterlockedDecrement(&This->numIfaces))
528 ddraw_destroy(This);
530 return ref;
533 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
535 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
536 ULONG ref = InterlockedDecrement(&This->ref2);
538 TRACE("%p decreasing refcount to %u.\n", This, ref);
540 if (!ref && !InterlockedDecrement(&This->numIfaces))
541 ddraw_destroy(This);
543 return ref;
546 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
548 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
549 ULONG ref = InterlockedDecrement(&This->ref1);
551 TRACE("%p decreasing refcount to %u.\n", This, ref);
553 if (!ref && !InterlockedDecrement(&This->numIfaces))
554 ddraw_destroy(This);
556 return ref;
559 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
561 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
563 TRACE("iface %p.\n", iface);
565 return ddraw7_Release(&This->IDirectDraw7_iface);
568 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
570 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
572 TRACE("iface %p.\n", iface);
574 return ddraw1_Release(&This->IDirectDraw_iface);
577 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
579 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
581 TRACE("iface %p.\n", iface);
583 return ddraw1_Release(&This->IDirectDraw_iface);
586 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
588 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
590 TRACE("iface %p.\n", iface);
592 return ddraw1_Release(&This->IDirectDraw_iface);
595 /*****************************************************************************
596 * IDirectDraw methods
597 *****************************************************************************/
599 /*****************************************************************************
600 * IDirectDraw7::SetCooperativeLevel
602 * Sets the cooperative level for the DirectDraw object, and the window
603 * assigned to it. The cooperative level determines the general behavior
604 * of the DirectDraw application
606 * Warning: This is quite tricky, as it's not really documented which
607 * cooperative levels can be combined with each other. If a game fails
608 * after this function, try to check the cooperative levels passed on
609 * Windows, and if it returns something different.
611 * If you think that this function caused the failure because it writes a
612 * fixme, be sure to run again with a +ddraw trace.
614 * What is known about cooperative levels (See the ddraw modes test):
615 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
616 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
617 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
618 * DDSCL_FULLSCREEN can be activated
619 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
621 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
622 * DDSCL_SETFOCUSWINDOW (partially),
623 * DDSCL_MULTITHREADED (work in progress)
625 * Unhandled flags, which should be implemented
626 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
627 * expect any difference to a normal window for wine)
628 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
629 * rendering (Possible test case: Half-Life)
631 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
633 * These don't seem very important for wine:
634 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
636 * Returns:
637 * DD_OK if the cooperative level was set successfully
638 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
639 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
640 * (Probably others too, have to investigate)
642 *****************************************************************************/
643 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
645 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
646 HWND window;
648 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
649 DDRAW_dump_cooperativelevel(cooplevel);
651 EnterCriticalSection(&ddraw_cs);
653 /* Get the old window */
654 window = This->dest_window;
656 /* Tests suggest that we need one of them: */
657 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
658 DDSCL_NORMAL |
659 DDSCL_EXCLUSIVE )))
661 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
662 LeaveCriticalSection(&ddraw_cs);
663 return DDERR_INVALIDPARAMS;
666 /* Handle those levels first which set various hwnds */
667 if(cooplevel & DDSCL_SETFOCUSWINDOW)
669 /* This isn't compatible with a lot of flags */
670 if(cooplevel & ( DDSCL_MULTITHREADED |
671 DDSCL_CREATEDEVICEWINDOW |
672 DDSCL_FPUSETUP |
673 DDSCL_FPUPRESERVE |
674 DDSCL_ALLOWREBOOT |
675 DDSCL_ALLOWMODEX |
676 DDSCL_SETDEVICEWINDOW |
677 DDSCL_NORMAL |
678 DDSCL_EXCLUSIVE |
679 DDSCL_FULLSCREEN ) )
681 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
682 LeaveCriticalSection(&ddraw_cs);
683 return DDERR_INVALIDPARAMS;
686 if( (This->cooperative_level & DDSCL_EXCLUSIVE) && window )
688 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
689 LeaveCriticalSection(&ddraw_cs);
690 return DDERR_HWNDALREADYSET;
693 This->focuswindow = hwnd;
694 /* Won't use the hwnd param for anything else */
695 hwnd = NULL;
697 /* Use the focus window for drawing too */
698 This->dest_window = This->focuswindow;
700 /* Destroy the device window, if we have one */
701 if(This->devicewindow)
703 DestroyWindow(This->devicewindow);
704 This->devicewindow = NULL;
707 LeaveCriticalSection(&ddraw_cs);
708 return DD_OK;
711 if(cooplevel & DDSCL_EXCLUSIVE)
713 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
715 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
716 LeaveCriticalSection(&ddraw_cs);
717 return DDERR_INVALIDPARAMS;
720 else if( !(cooplevel & DDSCL_NORMAL) )
722 TRACE("(%p) SetCooperativeLevel needs at least SetFocusWindow or Exclusive or Normal mode\n", This);
723 LeaveCriticalSection(&ddraw_cs);
724 return DDERR_INVALIDPARAMS;
727 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
728 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
729 wined3d_device_release_focus_window(This->wined3d_device);
731 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
733 if (This->cooperative_level & DDSCL_FULLSCREEN)
734 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
736 if (cooplevel & DDSCL_FULLSCREEN)
738 WINED3DDISPLAYMODE display_mode;
740 wined3d_get_adapter_display_mode(This->wineD3D, WINED3DADAPTER_DEFAULT, &display_mode);
741 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
742 display_mode.Width, display_mode.Height);
746 if ((cooplevel & DDSCL_EXCLUSIVE)
747 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
749 HRESULT hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
750 if (FAILED(hr))
752 ERR("Failed to acquire focus window, hr %#x.\n", hr);
753 LeaveCriticalSection(&ddraw_cs);
754 return hr;
758 /* Don't override focus windows or private device windows */
759 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
760 This->dest_window = hwnd;
762 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
764 /* Don't create a device window if a focus window is set */
765 if( !(This->focuswindow) )
767 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
768 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
769 NULL, NULL, NULL, NULL);
770 if (!devicewindow)
772 ERR("Failed to create window, last error %#x.\n", GetLastError());
773 LeaveCriticalSection(&ddraw_cs);
774 return E_FAIL;
777 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
778 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
780 This->devicewindow = devicewindow;
781 This->dest_window = devicewindow;
785 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
786 wined3d_device_set_multithreaded(This->wined3d_device);
788 /* Unhandled flags */
789 if(cooplevel & DDSCL_ALLOWREBOOT)
790 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
791 if(cooplevel & DDSCL_ALLOWMODEX)
792 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
793 if(cooplevel & DDSCL_FPUSETUP)
794 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
796 /* Store the cooperative_level */
797 This->cooperative_level = cooplevel;
798 TRACE("SetCooperativeLevel retuning DD_OK\n");
799 LeaveCriticalSection(&ddraw_cs);
800 return DD_OK;
803 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
805 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
807 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
809 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
812 static HRESULT WINAPI ddraw3_SetCooperativeLevel(IDirectDraw3 *iface, HWND window, DWORD flags)
814 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
816 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
818 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
821 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
823 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
825 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
827 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
830 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
832 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
834 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
836 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
839 /*****************************************************************************
841 * Helper function for SetDisplayMode and RestoreDisplayMode
843 * Implements DirectDraw's SetDisplayMode, but ignores the value of
844 * ForceRefreshRate, since it is already handled by
845 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
846 * without worrying that ForceRefreshRate will override the refresh rate. For
847 * argument and return value documentation, see
848 * ddraw7_SetDisplayMode.
850 *****************************************************************************/
851 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
852 DWORD BPP, DWORD RefreshRate, DWORD Flags)
854 enum wined3d_format_id format;
855 WINED3DDISPLAYMODE Mode;
856 HRESULT hr;
858 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
859 Height, BPP, RefreshRate, Flags);
861 EnterCriticalSection(&ddraw_cs);
862 if( !Width || !Height )
864 ERR("Width %u, Height %u, what to do?\n", Width, Height);
865 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
866 LeaveCriticalSection(&ddraw_cs);
867 return DD_OK;
870 switch(BPP)
872 case 8: format = WINED3DFMT_P8_UINT; break;
873 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
874 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
875 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
876 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
877 default: format = WINED3DFMT_UNKNOWN; break;
880 if (FAILED(hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode)))
882 ERR("Failed to get current display mode, hr %#x.\n", hr);
884 else if (Mode.Width == Width
885 && Mode.Height == Height
886 && Mode.Format == format
887 && Mode.RefreshRate == RefreshRate)
889 TRACE("Skipping redundant mode setting call.\n");
890 LeaveCriticalSection(&ddraw_cs);
891 return DD_OK;
894 /* Check the exclusive mode
895 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
896 return DDERR_NOEXCLUSIVEMODE;
897 * This is WRONG. Don't know if the SDK is completely
898 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
899 * is returned, but Half-Life 1.1.1.1 (Steam version)
900 * depends on this
903 Mode.Width = Width;
904 Mode.Height = Height;
905 Mode.RefreshRate = RefreshRate;
906 Mode.Format = format;
908 /* TODO: The possible return values from msdn suggest that
909 * the screen mode can't be changed if a surface is locked
910 * or some drawing is in progress
913 /* TODO: Lose the primary surface */
914 hr = wined3d_device_set_display_mode(ddraw->wined3d_device, 0, &Mode);
916 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
918 wined3d_device_restore_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window);
919 wined3d_device_setup_fullscreen_window(ddraw->wined3d_device, ddraw->dest_window, Width, Height);
922 LeaveCriticalSection(&ddraw_cs);
923 switch(hr)
925 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
926 default: return hr;
930 /*****************************************************************************
931 * IDirectDraw7::SetDisplayMode
933 * Sets the display screen resolution, color depth and refresh frequency
934 * when in fullscreen mode (in theory).
935 * Possible return values listed in the SDK suggest that this method fails
936 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
937 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
938 * It seems to be valid to pass 0 for With and Height, this has to be tested
939 * It could mean that the current video mode should be left as-is. (But why
940 * call it then?)
942 * Params:
943 * Height, Width: Screen dimension
944 * BPP: Color depth in Bits per pixel
945 * Refreshrate: Screen refresh rate
946 * Flags: Other stuff
948 * Returns
949 * DD_OK on success
951 *****************************************************************************/
952 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
953 DWORD BPP, DWORD RefreshRate, DWORD Flags)
955 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
957 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
958 iface, Width, Height, BPP, RefreshRate, Flags);
960 if (force_refresh_rate != 0)
962 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
963 RefreshRate, force_refresh_rate);
964 RefreshRate = force_refresh_rate;
967 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
970 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
971 DWORD bpp, DWORD refresh_rate, DWORD flags)
973 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
975 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
976 iface, width, height, bpp, refresh_rate, flags);
978 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
981 static HRESULT WINAPI ddraw3_SetDisplayMode(IDirectDraw3 *iface, DWORD width, DWORD height,
982 DWORD bpp, DWORD refresh_rate, DWORD flags)
984 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
986 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
987 iface, width, height, bpp, refresh_rate, flags);
989 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
992 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
993 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
995 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
997 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
998 iface, width, height, bpp, refresh_rate, flags);
1000 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1003 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1005 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1007 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1009 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, 0, 0);
1012 /*****************************************************************************
1013 * IDirectDraw7::RestoreDisplayMode
1015 * Restores the display mode to what it was at creation time. Basically.
1017 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1018 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1019 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1020 * -> DD_1 is released. The screen should be left at 1024x768x32.
1021 * -> DD_2 is released. The screen should be set to 1400x1050x32
1022 * This case is unhandled right now, but Empire Earth does it this way.
1023 * (But perhaps there is something in SetCooperativeLevel to prevent this)
1025 * The msdn says that this method resets the display mode to what it was before
1026 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1028 * Returns
1029 * DD_OK on success
1030 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1032 *****************************************************************************/
1033 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1035 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1037 TRACE("iface %p.\n", iface);
1039 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
1042 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1044 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1046 TRACE("iface %p.\n", iface);
1048 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1051 static HRESULT WINAPI ddraw3_RestoreDisplayMode(IDirectDraw3 *iface)
1053 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1055 TRACE("iface %p.\n", iface);
1057 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1060 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1062 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1064 TRACE("iface %p.\n", iface);
1066 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1069 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1071 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1073 TRACE("iface %p.\n", iface);
1075 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1078 /*****************************************************************************
1079 * IDirectDraw7::GetCaps
1081 * Returns the drives capabilities
1083 * Used for version 1, 2, 4 and 7
1085 * Params:
1086 * DriverCaps: Structure to write the Hardware accelerated caps to
1087 * HelCaps: Structure to write the emulation caps to
1089 * Returns
1090 * This implementation returns DD_OK only
1092 *****************************************************************************/
1093 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1095 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1096 DDCAPS caps;
1097 WINED3DCAPS winecaps;
1098 HRESULT hr;
1099 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1101 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1103 /* One structure must be != NULL */
1104 if( (!DriverCaps) && (!HELCaps) )
1106 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1107 return DDERR_INVALIDPARAMS;
1110 memset(&caps, 0, sizeof(caps));
1111 memset(&winecaps, 0, sizeof(winecaps));
1112 caps.dwSize = sizeof(caps);
1113 EnterCriticalSection(&ddraw_cs);
1114 hr = wined3d_device_get_device_caps(This->wined3d_device, &winecaps);
1115 if (FAILED(hr))
1117 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1118 LeaveCriticalSection(&ddraw_cs);
1119 return hr;
1122 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1123 LeaveCriticalSection(&ddraw_cs);
1124 if(FAILED(hr)) {
1125 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1126 return hr;
1129 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1130 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1131 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1132 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1133 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1134 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1135 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1136 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1137 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1138 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1139 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1140 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1141 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1142 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1143 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1145 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1146 * not to use it
1148 if(DefaultSurfaceType == SURFACE_GDI) {
1149 caps.dwCaps &= ~DDCAPS_3D;
1150 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1152 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1153 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1154 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1157 if(DriverCaps)
1159 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1160 if (TRACE_ON(ddraw))
1162 TRACE("Driver Caps :\n");
1163 DDRAW_dump_DDCAPS(DriverCaps);
1167 if(HELCaps)
1169 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1170 if (TRACE_ON(ddraw))
1172 TRACE("HEL Caps :\n");
1173 DDRAW_dump_DDCAPS(HELCaps);
1177 return DD_OK;
1180 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1182 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1184 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1186 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1189 static HRESULT WINAPI ddraw3_GetCaps(IDirectDraw3 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1191 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1193 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1195 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1198 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1200 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1202 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1204 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1207 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1209 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1211 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1213 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1216 /*****************************************************************************
1217 * IDirectDraw7::Compact
1219 * No idea what it does, MSDN says it's not implemented.
1221 * Returns
1222 * DD_OK, but this is unchecked
1224 *****************************************************************************/
1225 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1227 TRACE("iface %p.\n", iface);
1229 return DD_OK;
1232 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1234 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1236 TRACE("iface %p.\n", iface);
1238 return ddraw7_Compact(&This->IDirectDraw7_iface);
1241 static HRESULT WINAPI ddraw3_Compact(IDirectDraw3 *iface)
1243 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1245 TRACE("iface %p.\n", iface);
1247 return ddraw7_Compact(&This->IDirectDraw7_iface);
1250 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1252 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1254 TRACE("iface %p.\n", iface);
1256 return ddraw7_Compact(&This->IDirectDraw7_iface);
1259 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1261 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1263 TRACE("iface %p.\n", iface);
1265 return ddraw7_Compact(&This->IDirectDraw7_iface);
1268 /*****************************************************************************
1269 * IDirectDraw7::GetDisplayMode
1271 * Returns information about the current display mode
1273 * Exists in Version 1, 2, 4 and 7
1275 * Params:
1276 * DDSD: Address of a surface description structure to write the info to
1278 * Returns
1279 * DD_OK
1281 *****************************************************************************/
1282 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1284 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1285 HRESULT hr;
1286 WINED3DDISPLAYMODE Mode;
1287 DWORD Size;
1289 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1291 EnterCriticalSection(&ddraw_cs);
1292 /* This seems sane */
1293 if (!DDSD)
1295 LeaveCriticalSection(&ddraw_cs);
1296 return DDERR_INVALIDPARAMS;
1299 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1300 * so one method can be used for all versions (Hopefully) */
1301 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1302 if (FAILED(hr))
1304 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1305 LeaveCriticalSection(&ddraw_cs);
1306 return hr;
1309 Size = DDSD->dwSize;
1310 memset(DDSD, 0, Size);
1312 DDSD->dwSize = Size;
1313 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1314 DDSD->dwWidth = Mode.Width;
1315 DDSD->dwHeight = Mode.Height;
1316 DDSD->u2.dwRefreshRate = 60;
1317 DDSD->ddsCaps.dwCaps = 0;
1318 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1319 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
1320 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1322 if(TRACE_ON(ddraw))
1324 TRACE("Returning surface desc :\n");
1325 DDRAW_dump_surface_desc(DDSD);
1328 LeaveCriticalSection(&ddraw_cs);
1329 return DD_OK;
1332 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1334 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1336 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1338 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, surface_desc);
1341 static HRESULT WINAPI ddraw3_GetDisplayMode(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc)
1343 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1345 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1347 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1350 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1352 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1354 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1356 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1359 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1361 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1363 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1365 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1368 /*****************************************************************************
1369 * IDirectDraw7::GetFourCCCodes
1371 * Returns an array of supported FourCC codes.
1373 * Exists in Version 1, 2, 4 and 7
1375 * Params:
1376 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1377 * of enumerated codes
1378 * Codes: Pointer to an array of DWORDs where the supported codes are written
1379 * to
1381 * Returns
1382 * Always returns DD_OK, as it's a stub for now
1384 *****************************************************************************/
1385 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1387 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1388 static const enum wined3d_format_id formats[] =
1390 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1391 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1392 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1394 DWORD count = 0, i, outsize;
1395 HRESULT hr;
1396 WINED3DDISPLAYMODE d3ddm;
1397 WINED3DSURFTYPE type = This->ImplType;
1399 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1401 wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
1403 outsize = NumCodes && Codes ? *NumCodes : 0;
1405 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
1407 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1409 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, WINED3DDEVTYPE_HAL,
1410 d3ddm.Format, 0, WINED3DRTYPE_SURFACE, formats[i], type);
1411 if (SUCCEEDED(hr))
1413 if (count < outsize)
1414 Codes[count] = formats[i];
1415 ++count;
1418 if(NumCodes) {
1419 TRACE("Returning %u FourCC codes\n", count);
1420 *NumCodes = count;
1423 return DD_OK;
1426 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1428 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1430 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1432 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1435 static HRESULT WINAPI ddraw3_GetFourCCCodes(IDirectDraw3 *iface, DWORD *codes_count, DWORD *codes)
1437 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1439 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1441 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1444 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1446 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1448 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1450 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1453 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1455 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1457 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1459 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1462 /*****************************************************************************
1463 * IDirectDraw7::GetMonitorFrequency
1465 * Returns the monitor's frequency
1467 * Exists in Version 1, 2, 4 and 7
1469 * Params:
1470 * Freq: Pointer to a DWORD to write the frequency to
1472 * Returns
1473 * Always returns DD_OK
1475 *****************************************************************************/
1476 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1478 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1480 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1481 * but for now this should make the games happy
1483 *Freq = 60;
1484 return DD_OK;
1487 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1489 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1491 TRACE("iface %p, frequency %p.\n", iface, frequency);
1493 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1496 static HRESULT WINAPI ddraw3_GetMonitorFrequency(IDirectDraw3 *iface, DWORD *frequency)
1498 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1500 TRACE("iface %p, frequency %p.\n", iface, frequency);
1502 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1505 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1507 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1509 TRACE("iface %p, frequency %p.\n", iface, frequency);
1511 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1514 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1516 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1518 TRACE("iface %p, frequency %p.\n", iface, frequency);
1520 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1523 /*****************************************************************************
1524 * IDirectDraw7::GetVerticalBlankStatus
1526 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1527 * too basically, but as it's a semi stub, I didn't create a function there
1529 * Params:
1530 * status: Pointer to a BOOL to be filled with the vertical blank status
1532 * Returns
1533 * DD_OK on success
1534 * DDERR_INVALIDPARAMS if status is NULL
1536 *****************************************************************************/
1537 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1539 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1541 TRACE("iface %p, status %p.\n", iface, status);
1543 /* This looks sane, the MSDN suggests it too */
1544 EnterCriticalSection(&ddraw_cs);
1545 if(!status)
1547 LeaveCriticalSection(&ddraw_cs);
1548 return DDERR_INVALIDPARAMS;
1551 *status = This->fake_vblank;
1552 This->fake_vblank = !This->fake_vblank;
1553 LeaveCriticalSection(&ddraw_cs);
1554 return DD_OK;
1557 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1559 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1561 TRACE("iface %p, status %p.\n", iface, status);
1563 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1566 static HRESULT WINAPI ddraw3_GetVerticalBlankStatus(IDirectDraw3 *iface, BOOL *status)
1568 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1570 TRACE("iface %p, status %p.\n", iface, status);
1572 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1575 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1577 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1579 TRACE("iface %p, status %p.\n", iface, status);
1581 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1584 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1586 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1588 TRACE("iface %p, status %p.\n", iface, status);
1590 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1593 /*****************************************************************************
1594 * IDirectDraw7::GetAvailableVidMem
1596 * Returns the total and free video memory
1598 * Params:
1599 * Caps: Specifies the memory type asked for
1600 * total: Pointer to a DWORD to be filled with the total memory
1601 * free: Pointer to a DWORD to be filled with the free memory
1603 * Returns
1604 * DD_OK on success
1605 * DDERR_INVALIDPARAMS of free and total are NULL
1607 *****************************************************************************/
1608 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1609 DWORD *free)
1611 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1613 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1615 if(TRACE_ON(ddraw))
1617 TRACE("(%p) Asked for memory with description: ", This);
1618 DDRAW_dump_DDSCAPS2(Caps);
1620 EnterCriticalSection(&ddraw_cs);
1622 /* Todo: System memory vs local video memory vs non-local video memory
1623 * The MSDN also mentions differences between texture memory and other
1624 * resources, but that's not important
1627 if( (!total) && (!free) )
1629 LeaveCriticalSection(&ddraw_cs);
1630 return DDERR_INVALIDPARAMS;
1633 if (total)
1634 *total = This->total_vidmem;
1635 if (free)
1636 *free = wined3d_device_get_available_texture_mem(This->wined3d_device);
1638 LeaveCriticalSection(&ddraw_cs);
1639 return DD_OK;
1642 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1643 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1645 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1647 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1649 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, caps, total, free);
1652 static HRESULT WINAPI ddraw3_GetAvailableVidMem(IDirectDraw3 *iface, DDSCAPS *caps, DWORD *total,
1653 DWORD *free)
1655 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1656 DDSCAPS2 caps2;
1658 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1660 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1661 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1664 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1665 DDSCAPS *caps, DWORD *total, DWORD *free)
1667 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1668 DDSCAPS2 caps2;
1670 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1672 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1673 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1676 /*****************************************************************************
1677 * IDirectDraw7::Initialize
1679 * Initializes a DirectDraw interface.
1681 * Params:
1682 * GUID: Interface identifier. Well, don't know what this is really good
1683 * for
1685 * Returns
1686 * Returns DD_OK on the first call,
1687 * DDERR_ALREADYINITIALIZED on repeated calls
1689 *****************************************************************************/
1690 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *Guid)
1692 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1694 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(Guid));
1696 if(This->initialized)
1698 return DDERR_ALREADYINITIALIZED;
1700 else
1702 return DD_OK;
1706 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1708 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1710 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1712 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1715 static HRESULT WINAPI ddraw3_Initialize(IDirectDraw3 *iface, GUID *guid)
1717 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1719 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1721 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1724 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1726 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1728 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1730 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1733 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1735 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1737 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1739 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1742 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1744 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1746 return D3D_OK;
1749 /*****************************************************************************
1750 * IDirectDraw7::FlipToGDISurface
1752 * "Makes the surface that the GDI writes to the primary surface"
1753 * Looks like some windows specific thing we don't have to care about.
1754 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1755 * show error boxes ;)
1756 * Well, just return DD_OK.
1758 * Returns:
1759 * Always returns DD_OK
1761 *****************************************************************************/
1762 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1764 FIXME("iface %p stub!\n", iface);
1766 return DD_OK;
1769 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1771 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1773 TRACE("iface %p.\n", iface);
1775 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1778 static HRESULT WINAPI ddraw3_FlipToGDISurface(IDirectDraw3 *iface)
1780 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1782 TRACE("iface %p.\n", iface);
1784 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1787 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1789 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1791 TRACE("iface %p.\n", iface);
1793 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1796 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1798 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1800 TRACE("iface %p.\n", iface);
1802 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1805 /*****************************************************************************
1806 * IDirectDraw7::WaitForVerticalBlank
1808 * This method allows applications to get in sync with the vertical blank
1809 * interval.
1810 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1811 * redraw the screen, most likely because of this stub
1813 * Parameters:
1814 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1815 * or DDWAITVB_BLOCKEND
1816 * h: Not used, according to MSDN
1818 * Returns:
1819 * Always returns DD_OK
1821 *****************************************************************************/
1822 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1824 static BOOL hide;
1826 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1828 /* This function is called often, so print the fixme only once */
1829 if(!hide)
1831 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1832 hide = TRUE;
1835 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1836 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1837 return DDERR_UNSUPPORTED; /* unchecked */
1839 return DD_OK;
1842 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1844 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1846 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1848 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1851 static HRESULT WINAPI ddraw3_WaitForVerticalBlank(IDirectDraw3 *iface, DWORD flags, HANDLE event)
1853 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1855 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1857 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1860 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1862 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1864 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1866 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1869 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1871 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1873 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1875 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1878 /*****************************************************************************
1879 * IDirectDraw7::GetScanLine
1881 * Returns the scan line that is being drawn on the monitor
1883 * Parameters:
1884 * Scanline: Address to write the scan line value to
1886 * Returns:
1887 * Always returns DD_OK
1889 *****************************************************************************/
1890 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1892 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1893 static BOOL hide = FALSE;
1894 WINED3DDISPLAYMODE Mode;
1896 TRACE("iface %p, line %p.\n", iface, Scanline);
1898 /* This function is called often, so print the fixme only once */
1899 EnterCriticalSection(&ddraw_cs);
1900 if(!hide)
1902 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1903 hide = TRUE;
1906 wined3d_device_get_display_mode(This->wined3d_device, 0, &Mode);
1908 /* Fake the line sweeping of the monitor */
1909 /* FIXME: We should synchronize with a source to keep the refresh rate */
1910 *Scanline = This->cur_scanline++;
1911 /* Assume 20 scan lines in the vertical blank */
1912 if (This->cur_scanline >= Mode.Height + 20)
1913 This->cur_scanline = 0;
1915 LeaveCriticalSection(&ddraw_cs);
1916 return DD_OK;
1919 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1921 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1923 TRACE("iface %p, line %p.\n", iface, line);
1925 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1928 static HRESULT WINAPI ddraw3_GetScanLine(IDirectDraw3 *iface, DWORD *line)
1930 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
1932 TRACE("iface %p, line %p.\n", iface, line);
1934 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1937 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1939 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1941 TRACE("iface %p, line %p.\n", iface, line);
1943 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1946 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1948 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1950 TRACE("iface %p, line %p.\n", iface, line);
1952 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1955 /*****************************************************************************
1956 * IDirectDraw7::TestCooperativeLevel
1958 * Informs the application about the state of the video adapter, depending
1959 * on the cooperative level
1961 * Returns:
1962 * DD_OK if the device is in a sane state
1963 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1964 * if the state is not correct(See below)
1966 *****************************************************************************/
1967 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1969 TRACE("iface %p.\n", iface);
1971 return DD_OK;
1974 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1976 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1978 TRACE("iface %p.\n", iface);
1980 return ddraw7_TestCooperativeLevel(&This->IDirectDraw7_iface);
1983 /*****************************************************************************
1984 * IDirectDraw7::GetGDISurface
1986 * Returns the surface that GDI is treating as the primary surface.
1987 * For Wine this is the front buffer
1989 * Params:
1990 * GDISurface: Address to write the surface pointer to
1992 * Returns:
1993 * DD_OK if the surface was found
1994 * DDERR_NOTFOUND if the GDI surface wasn't found
1996 *****************************************************************************/
1997 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1999 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2000 struct wined3d_surface *wined3d_surface;
2001 IDirectDrawSurface7 *ddsurf;
2002 HRESULT hr;
2003 DDSCAPS2 ddsCaps;
2005 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2007 /* Get the back buffer from the wineD3DDevice and search its
2008 * attached surfaces for the front buffer. */
2009 EnterCriticalSection(&ddraw_cs);
2010 hr = wined3d_device_get_back_buffer(This->wined3d_device,
2011 0, 0, WINED3DBACKBUFFER_TYPE_MONO, &wined3d_surface);
2012 if (FAILED(hr) || !wined3d_surface)
2014 ERR("IWineD3DDevice::GetBackBuffer failed\n");
2015 LeaveCriticalSection(&ddraw_cs);
2016 return DDERR_NOTFOUND;
2019 ddsurf = wined3d_surface_get_parent(wined3d_surface);
2020 wined3d_surface_decref(wined3d_surface);
2022 /* Find the front buffer */
2023 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
2024 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
2025 &ddsCaps,
2026 GDISurface);
2027 if(hr != DD_OK)
2029 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
2032 /* The AddRef is OK this time */
2033 LeaveCriticalSection(&ddraw_cs);
2034 return hr;
2037 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2039 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2040 IDirectDrawSurface7 *surface7;
2041 IDirectDrawSurfaceImpl *surface_impl;
2042 HRESULT hr;
2044 TRACE("iface %p, surface %p.\n", iface, surface);
2046 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2047 if (FAILED(hr))
2049 *surface = NULL;
2050 return hr;
2052 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2053 *surface = &surface_impl->IDirectDrawSurface4_iface;
2055 return hr;
2058 static HRESULT WINAPI ddraw3_GetGDISurface(IDirectDraw3 *iface, IDirectDrawSurface **surface)
2060 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2061 IDirectDrawSurface7 *surface7;
2062 IDirectDrawSurfaceImpl *surface_impl;
2063 HRESULT hr;
2065 TRACE("iface %p, surface %p.\n", iface, surface);
2067 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2068 if (FAILED(hr))
2070 *surface = NULL;
2071 return hr;
2073 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2074 *surface = (IDirectDrawSurface *)&surface_impl->IDirectDrawSurface3_iface;
2076 return hr;
2079 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2081 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2082 IDirectDrawSurface7 *surface7;
2083 IDirectDrawSurfaceImpl *surface_impl;
2084 HRESULT hr;
2086 TRACE("iface %p, surface %p.\n", iface, surface);
2088 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2089 if (FAILED(hr))
2091 *surface = NULL;
2092 return hr;
2094 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2095 *surface = (IDirectDrawSurface *)&surface_impl->IDirectDrawSurface3_iface;
2097 return hr;
2100 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2102 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2103 IDirectDrawSurface7 *surface7;
2104 IDirectDrawSurfaceImpl *surface_impl;
2105 HRESULT hr;
2107 TRACE("iface %p, surface %p.\n", iface, surface);
2109 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2110 if (FAILED(hr))
2112 *surface = NULL;
2113 return hr;
2115 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2116 *surface = (IDirectDrawSurface *)&surface_impl->IDirectDrawSurface3_iface;
2118 return hr;
2121 struct displaymodescallback_context
2123 LPDDENUMMODESCALLBACK func;
2124 void *context;
2127 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2129 struct displaymodescallback_context *cbcontext = context;
2130 DDSURFACEDESC desc;
2132 memcpy(&desc, surface_desc, sizeof(desc));
2133 desc.dwSize = sizeof(desc);
2135 return cbcontext->func(&desc, cbcontext->context);
2138 /*****************************************************************************
2139 * IDirectDraw7::EnumDisplayModes
2141 * Enumerates the supported Display modes. The modes can be filtered with
2142 * the DDSD parameter.
2144 * Params:
2145 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2146 * versions (3 and older?) this is reserved and must be 0.
2147 * DDSD: Surface description to filter the modes
2148 * Context: Pointer passed back to the callback function
2149 * cb: Application-provided callback function
2151 * Returns:
2152 * DD_OK on success
2153 * DDERR_INVALIDPARAMS if the callback wasn't set
2155 *****************************************************************************/
2156 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2157 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2159 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2160 unsigned int modenum, fmt;
2161 WINED3DDISPLAYMODE mode;
2162 DDSURFACEDESC2 callback_sd;
2163 WINED3DDISPLAYMODE *enum_modes = NULL;
2164 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2165 DDPIXELFORMAT pixelformat;
2167 static const enum wined3d_format_id checkFormatList[] =
2169 WINED3DFMT_B8G8R8X8_UNORM,
2170 WINED3DFMT_B5G6R5_UNORM,
2171 WINED3DFMT_P8_UINT,
2174 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2175 iface, Flags, DDSD, Context, cb);
2177 EnterCriticalSection(&ddraw_cs);
2178 /* This looks sane */
2179 if(!cb)
2181 LeaveCriticalSection(&ddraw_cs);
2182 return DDERR_INVALIDPARAMS;
2185 if(!(Flags & DDEDM_REFRESHRATES))
2187 enum_mode_array_size = 16;
2188 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2189 if (!enum_modes)
2191 ERR("Out of memory\n");
2192 LeaveCriticalSection(&ddraw_cs);
2193 return DDERR_OUTOFMEMORY;
2197 pixelformat.dwSize = sizeof(pixelformat);
2198 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2200 modenum = 0;
2201 while (wined3d_enum_adapter_modes(This->wineD3D, WINED3DADAPTER_DEFAULT,
2202 checkFormatList[fmt], modenum++, &mode) == WINED3D_OK)
2204 PixelFormat_WineD3DtoDD(&pixelformat, mode.Format);
2205 if(DDSD)
2207 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
2208 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
2209 if(DDSD->dwFlags & DDSD_REFRESHRATE && mode.RefreshRate != DDSD->u2.dwRefreshRate) continue;
2210 if (DDSD->dwFlags & DDSD_PIXELFORMAT &&
2211 pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount) continue;
2214 if(!(Flags & DDEDM_REFRESHRATES))
2216 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2217 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2218 * to be reduced to a single unique result in such case.
2220 BOOL found = FALSE;
2221 unsigned i;
2223 for (i = 0; i < enum_mode_count; i++)
2225 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
2226 enum_modes[i].Format == mode.Format)
2228 found = TRUE;
2229 break;
2233 if(found) continue;
2236 memset(&callback_sd, 0, sizeof(callback_sd));
2237 callback_sd.dwSize = sizeof(callback_sd);
2238 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2240 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2241 if(Flags & DDEDM_REFRESHRATES)
2243 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
2246 callback_sd.dwWidth = mode.Width;
2247 callback_sd.dwHeight = mode.Height;
2249 callback_sd.u4.ddpfPixelFormat=pixelformat;
2251 /* Calc pitch and DWORD align like MSDN says */
2252 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
2253 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2255 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2256 callback_sd.u2.dwRefreshRate);
2258 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2260 TRACE("Application asked to terminate the enumeration\n");
2261 HeapFree(GetProcessHeap(), 0, enum_modes);
2262 LeaveCriticalSection(&ddraw_cs);
2263 return DD_OK;
2266 if(!(Flags & DDEDM_REFRESHRATES))
2268 if (enum_mode_count == enum_mode_array_size)
2270 WINED3DDISPLAYMODE *new_enum_modes;
2272 enum_mode_array_size *= 2;
2273 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2275 if (!new_enum_modes)
2277 ERR("Out of memory\n");
2278 HeapFree(GetProcessHeap(), 0, enum_modes);
2279 LeaveCriticalSection(&ddraw_cs);
2280 return DDERR_OUTOFMEMORY;
2283 enum_modes = new_enum_modes;
2286 enum_modes[enum_mode_count++] = mode;
2291 TRACE("End of enumeration\n");
2292 HeapFree(GetProcessHeap(), 0, enum_modes);
2293 LeaveCriticalSection(&ddraw_cs);
2294 return DD_OK;
2297 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2298 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2300 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2302 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2303 iface, flags, surface_desc, context, callback);
2305 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, surface_desc, context, callback);
2308 static HRESULT WINAPI ddraw3_EnumDisplayModes(IDirectDraw3 *iface, DWORD flags,
2309 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2311 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2312 struct displaymodescallback_context cbcontext;
2314 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2315 iface, flags, surface_desc, context, callback);
2317 cbcontext.func = callback;
2318 cbcontext.context = context;
2320 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2321 &cbcontext, EnumDisplayModesCallbackThunk);
2324 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2325 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2327 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2328 struct displaymodescallback_context cbcontext;
2330 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2331 iface, flags, surface_desc, context, callback);
2333 cbcontext.func = callback;
2334 cbcontext.context = context;
2336 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2337 &cbcontext, EnumDisplayModesCallbackThunk);
2340 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2341 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2343 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2344 struct displaymodescallback_context cbcontext;
2346 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2347 iface, flags, surface_desc, context, callback);
2349 cbcontext.func = callback;
2350 cbcontext.context = context;
2352 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
2353 &cbcontext, EnumDisplayModesCallbackThunk);
2356 /*****************************************************************************
2357 * IDirectDraw7::EvaluateMode
2359 * Used with IDirectDraw7::StartModeTest to test video modes.
2360 * EvaluateMode is used to pass or fail a mode, and continue with the next
2361 * mode
2363 * Params:
2364 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2365 * Timeout: Returns the amount of seconds left before the mode would have
2366 * been failed automatically
2368 * Returns:
2369 * This implementation always DD_OK, because it's a stub
2371 *****************************************************************************/
2372 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2374 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2376 /* When implementing this, implement it in WineD3D */
2378 return DD_OK;
2381 /*****************************************************************************
2382 * IDirectDraw7::GetDeviceIdentifier
2384 * Returns the device identifier, which gives information about the driver
2385 * Our device identifier is defined at the beginning of this file.
2387 * Params:
2388 * DDDI: Address for the returned structure
2389 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2391 * Returns:
2392 * On success it returns DD_OK
2393 * DDERR_INVALIDPARAMS if DDDI is NULL
2395 *****************************************************************************/
2396 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2397 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2399 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2401 if(!DDDI)
2402 return DDERR_INVALIDPARAMS;
2404 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2405 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2406 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2407 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2408 * bytes too long. So only copy the relevant part of the structure
2411 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2412 return DD_OK;
2415 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2416 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2418 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2419 DDDEVICEIDENTIFIER2 identifier2;
2420 HRESULT hr;
2422 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2424 hr = ddraw7_GetDeviceIdentifier(&This->IDirectDraw7_iface, &identifier2, flags);
2425 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2427 return hr;
2430 /*****************************************************************************
2431 * IDirectDraw7::GetSurfaceFromDC
2433 * Returns the Surface for a GDI device context handle.
2434 * Is this related to IDirectDrawSurface::GetDC ???
2436 * Params:
2437 * hdc: hdc to return the surface for
2438 * Surface: Address to write the surface pointer to
2440 * Returns:
2441 * Always returns DD_OK because it's a stub
2443 *****************************************************************************/
2444 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2445 IDirectDrawSurface7 **Surface)
2447 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2448 struct wined3d_surface *wined3d_surface;
2449 HRESULT hr;
2451 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2453 if (!Surface) return E_INVALIDARG;
2455 hr = wined3d_device_get_surface_from_dc(This->wined3d_device, hdc, &wined3d_surface);
2456 if (FAILED(hr))
2458 TRACE("No surface found for dc %p.\n", hdc);
2459 *Surface = NULL;
2460 return DDERR_NOTFOUND;
2463 *Surface = wined3d_surface_get_parent(wined3d_surface);
2464 IDirectDrawSurface7_AddRef(*Surface);
2465 TRACE("Returning surface %p.\n", Surface);
2466 return DD_OK;
2469 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2470 IDirectDrawSurface4 **surface)
2472 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2473 IDirectDrawSurface7 *surface7;
2474 IDirectDrawSurfaceImpl *surface_impl;
2475 HRESULT hr;
2477 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2479 if (!surface) return E_INVALIDARG;
2481 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2482 if (FAILED(hr))
2484 *surface = NULL;
2485 return hr;
2487 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2488 /* Tests say this is true */
2489 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface3_iface;
2491 return hr;
2494 static HRESULT WINAPI ddraw3_GetSurfaceFromDC(IDirectDraw3 *iface, HDC dc,
2495 IDirectDrawSurface **surface)
2497 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
2498 IDirectDrawSurface7 *surface7;
2499 IDirectDrawSurfaceImpl *surface_impl;
2500 HRESULT hr;
2502 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2504 if (!surface) return E_INVALIDARG;
2506 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2507 if (FAILED(hr))
2509 *surface = NULL;
2510 return hr;
2513 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2514 *surface = (IDirectDrawSurface *)&surface_impl->IDirectDrawSurface3_iface;
2516 return hr;
2519 /*****************************************************************************
2520 * IDirectDraw7::RestoreAllSurfaces
2522 * Calls the restore method of all surfaces
2524 * Params:
2526 * Returns:
2527 * Always returns DD_OK because it's a stub
2529 *****************************************************************************/
2530 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2532 FIXME("iface %p stub!\n", iface);
2534 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2535 * get their parent and call their restore method. Do not implement
2536 * it in WineD3D, as restoring a surface means re-creating the
2537 * WineD3DDSurface
2539 return DD_OK;
2542 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2544 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2546 TRACE("iface %p.\n", iface);
2548 return ddraw7_RestoreAllSurfaces(&This->IDirectDraw7_iface);
2551 /*****************************************************************************
2552 * IDirectDraw7::StartModeTest
2554 * Tests the specified video modes to update the system registry with
2555 * refresh rate information. StartModeTest starts the mode test,
2556 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2557 * isn't called within 15 seconds, the mode is failed automatically
2559 * As refresh rates are handled by the X server, I don't think this
2560 * Method is important
2562 * Params:
2563 * Modes: An array of mode specifications
2564 * NumModes: The number of modes in Modes
2565 * Flags: Some flags...
2567 * Returns:
2568 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2569 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2570 * otherwise DD_OK
2572 *****************************************************************************/
2573 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2575 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2576 iface, Modes, NumModes, Flags);
2578 /* This looks sane */
2579 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2581 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2582 * As it is not, DDERR_TESTFINISHED is returned
2583 * (hopefully that's correct
2585 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2586 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2589 return DD_OK;
2592 /*****************************************************************************
2593 * ddraw_recreate_surfaces_cb
2595 * Enumeration callback for ddraw_recreate_surface.
2596 * It re-recreates the WineD3DSurface. It's pretty straightforward
2598 *****************************************************************************/
2599 HRESULT WINAPI ddraw_recreate_surfaces_cb(IDirectDrawSurface7 *surf, DDSURFACEDESC2 *desc, void *Context)
2601 IDirectDrawSurfaceImpl *surfImpl = impl_from_IDirectDrawSurface7(surf);
2602 struct wined3d_resource_desc wined3d_desc;
2603 struct wined3d_resource *wined3d_resource;
2604 IDirectDrawImpl *This = surfImpl->ddraw;
2605 struct wined3d_surface *wined3d_surface;
2606 struct wined3d_swapchain *swapchain;
2607 struct wined3d_clipper *clipper;
2608 void *parent;
2609 HRESULT hr;
2611 TRACE("surface %p, surface_desc %p, context %p.\n",
2612 surf, desc, Context);
2614 /* For the enumeration */
2615 IDirectDrawSurface7_Release(surf);
2617 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
2619 /* Get the objects */
2620 swapchain = surfImpl->wined3d_swapchain;
2621 surfImpl->wined3d_swapchain = NULL;
2622 wined3d_surface = surfImpl->wined3d_surface;
2624 /* get the clipper */
2625 clipper = wined3d_surface_get_clipper(wined3d_surface);
2627 /* Get the surface properties */
2628 wined3d_resource = wined3d_surface_get_resource(wined3d_surface);
2629 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
2631 parent = wined3d_surface_get_parent(wined3d_surface);
2632 hr = wined3d_surface_create(This->wined3d_device, wined3d_desc.width, wined3d_desc.height,
2633 wined3d_desc.format, TRUE, FALSE, surfImpl->mipmap_level, wined3d_desc.usage, wined3d_desc.pool,
2634 wined3d_desc.multisample_type, wined3d_desc.multisample_quality, This->ImplType,
2635 parent, &ddraw_surface_wined3d_parent_ops, &surfImpl->wined3d_surface);
2636 if (FAILED(hr))
2638 surfImpl->wined3d_surface = wined3d_surface;
2639 return hr;
2642 wined3d_surface_set_clipper(surfImpl->wined3d_surface, clipper);
2644 /* TODO: Copy the surface content, except for render targets */
2646 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
2647 * the swapchain
2649 if (swapchain)
2651 /* The backbuffers have the swapchain set as well, but the primary
2652 * owns it and destroys it. */
2653 if (surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2654 wined3d_device_uninit_gdi(This->wined3d_device);
2655 surfImpl->isRenderTarget = FALSE;
2657 else
2659 if (!wined3d_surface_decref(wined3d_surface))
2660 TRACE("Surface released successful, next surface\n");
2661 else
2662 ERR("Something's still holding the old WineD3DSurface\n");
2665 surfImpl->ImplType = This->ImplType;
2667 return DDENUMRET_OK;
2670 /*****************************************************************************
2671 * ddraw_recreate_surfaces
2673 * A function, that converts all wineD3DSurfaces to the new implementation type
2674 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
2675 * new WineD3DSurface, copies the content and releases the old surface
2677 *****************************************************************************/
2678 static HRESULT ddraw_recreate_surfaces(IDirectDrawImpl *This)
2680 DDSURFACEDESC2 desc;
2681 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
2683 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
2685 /* Should happen almost never */
2686 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
2687 /* Shutdown d3d */
2688 wined3d_device_uninit_3d(This->wined3d_device);
2690 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
2692 memset(&desc, 0, sizeof(desc));
2693 desc.dwSize = sizeof(desc);
2695 return IDirectDraw7_EnumSurfaces(&This->IDirectDraw7_iface, 0, &desc, This,
2696 ddraw_recreate_surfaces_cb);
2699 /*****************************************************************************
2700 * ddraw_create_surface
2702 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2703 * with the passed parameters.
2705 * Params:
2706 * DDSD: Description of the surface to create
2707 * Surf: Address to store the interface pointer at
2709 * Returns:
2710 * DD_OK on success
2712 *****************************************************************************/
2713 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2714 IDirectDrawSurfaceImpl **ppSurf, UINT level)
2716 WINED3DSURFTYPE ImplType = This->ImplType;
2717 HRESULT hr;
2719 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2720 This, pDDSD, ppSurf, level);
2722 if (TRACE_ON(ddraw))
2724 TRACE(" (%p) Requesting surface desc :\n", This);
2725 DDRAW_dump_surface_desc(pDDSD);
2728 /* Select the surface type, if it wasn't choosen yet */
2729 if(ImplType == SURFACE_UNKNOWN)
2731 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
2732 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2734 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
2735 ImplType = SURFACE_OPENGL;
2738 /* Otherwise use GDI surfaces for now */
2739 else
2741 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
2742 ImplType = SURFACE_GDI;
2745 /* Policy if all surface implementations are available:
2746 * First, check if a default type was set with winecfg. If not,
2747 * try Xrender surfaces, and use them if they work. Next, check if
2748 * accelerated OpenGL is available, and use GL surfaces in this
2749 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
2750 * was created, always use OpenGL surfaces.
2752 * (Note: Xrender surfaces are not implemented for now, the
2753 * unaccelerated implementation uses GDI to render in Software)
2756 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
2757 * be re-created. This could be done with IDirectDrawSurface7::Restore
2759 This->ImplType = ImplType;
2761 else
2763 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2764 && (This->ImplType != SURFACE_OPENGL)
2765 && DefaultSurfaceType == SURFACE_UNKNOWN)
2767 /* We have to change to OpenGL,
2768 * and re-create all WineD3DSurfaces
2770 ImplType = SURFACE_OPENGL;
2771 This->ImplType = ImplType;
2772 TRACE("(%p) Re-creating all surfaces\n", This);
2773 ddraw_recreate_surfaces(This);
2774 TRACE("(%p) Done recreating all surfaces\n", This);
2776 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2778 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2779 /* Do not fail surface creation, only fail 3D device creation */
2783 /* Create the Surface object */
2784 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2785 if(!*ppSurf)
2787 ERR("(%p) Error allocating memory for a surface\n", This);
2788 return DDERR_OUTOFVIDEOMEMORY;
2791 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, ImplType);
2792 if (FAILED(hr))
2794 WARN("Failed to initialize surface, hr %#x.\n", hr);
2795 HeapFree(GetProcessHeap(), 0, *ppSurf);
2796 return hr;
2799 /* Increase the surface counter, and attach the surface */
2800 InterlockedIncrement(&This->surfaces);
2801 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2803 TRACE("Created surface %p.\n", *ppSurf);
2805 return DD_OK;
2807 /*****************************************************************************
2808 * CreateAdditionalSurfaces
2810 * Creates a new mipmap chain.
2812 * Params:
2813 * root: Root surface to attach the newly created chain to
2814 * count: number of surfaces to create
2815 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2816 * effects on the caller
2817 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2818 * creates an additional surface without the mipmapping flags
2820 *****************************************************************************/
2821 static HRESULT
2822 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2823 IDirectDrawSurfaceImpl *root,
2824 UINT count,
2825 DDSURFACEDESC2 DDSD,
2826 BOOL CubeFaceRoot)
2828 UINT i, j, level = 0;
2829 HRESULT hr;
2830 IDirectDrawSurfaceImpl *last = root;
2832 for(i = 0; i < count; i++)
2834 IDirectDrawSurfaceImpl *object2 = NULL;
2836 /* increase the mipmap level, but only if a mipmap is created
2837 * In this case, also halve the size
2839 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2841 level++;
2842 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2843 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2844 /* Set the mipmap sublevel flag according to msdn */
2845 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2847 else
2849 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2851 CubeFaceRoot = FALSE;
2853 hr = ddraw_create_surface(This, &DDSD, &object2, level);
2854 if(hr != DD_OK)
2856 return hr;
2859 /* Add the new surface to the complex attachment array */
2860 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2862 if(last->complex_array[j]) continue;
2863 last->complex_array[j] = object2;
2864 break;
2866 last = object2;
2868 /* Remove the (possible) back buffer cap from the new surface description,
2869 * because only one surface in the flipping chain is a back buffer, one
2870 * is a front buffer, the others are just primary surfaces.
2872 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2874 return DD_OK;
2877 /* Must set all attached surfaces (e.g. mipmaps) versions as well */
2878 static void ddraw_set_surface_version(IDirectDrawSurfaceImpl *surface, UINT version)
2880 unsigned int i;
2882 TRACE("surface %p, version %u -> %u.\n", surface, surface->version, version);
2884 surface->version = version;
2885 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
2887 if (!surface->complex_array[i]) break;
2888 ddraw_set_surface_version(surface->complex_array[i], version);
2890 while ((surface = surface->next_attached))
2892 ddraw_set_surface_version(surface, version);
2896 /*****************************************************************************
2897 * ddraw_attach_d3d_device
2899 * Initializes the D3D capabilities of WineD3D
2901 * Params:
2902 * primary: The primary surface for D3D
2904 * Returns
2905 * DD_OK on success,
2906 * DDERR_* otherwise
2908 *****************************************************************************/
2909 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2911 WINED3DPRESENT_PARAMETERS localParameters;
2912 HWND window = ddraw->dest_window;
2913 HRESULT hr;
2915 TRACE("ddraw %p, primary %p.\n", ddraw, primary);
2917 if (!window || window == GetDesktopWindow())
2919 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2920 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2921 NULL, NULL, NULL, NULL);
2922 if (!window)
2924 ERR("Failed to create window, last error %#x.\n", GetLastError());
2925 return E_FAIL;
2928 ShowWindow(window, SW_HIDE); /* Just to be sure */
2929 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2931 else
2933 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2935 ddraw->d3d_window = window;
2937 /* Store the future Render Target surface */
2938 ddraw->d3d_target = primary;
2940 /* Use the surface description for the device parameters, not the device
2941 * settings. The application might render to an offscreen surface. */
2942 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2943 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2944 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2945 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2946 ? primary->surface_desc.dwBackBufferCount : 0;
2947 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2948 localParameters.MultiSampleQuality = 0;
2949 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2950 localParameters.hDeviceWindow = window;
2951 localParameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2952 localParameters.EnableAutoDepthStencil = TRUE;
2953 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2954 localParameters.Flags = 0;
2955 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2956 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2957 localParameters.AutoRestoreDisplayMode = FALSE;
2959 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2960 * recursive loop until ram or emulated video memory is full. */
2961 ddraw->d3d_initialized = TRUE;
2962 hr = wined3d_device_init_3d(ddraw->wined3d_device, &localParameters);
2963 if (FAILED(hr))
2965 ddraw->d3d_target = NULL;
2966 ddraw->d3d_initialized = FALSE;
2967 return hr;
2970 ddraw->declArraySize = 2;
2971 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2972 if (!ddraw->decls)
2974 ERR("Error allocating an array for the converted vertex decls.\n");
2975 ddraw->declArraySize = 0;
2976 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
2977 return E_OUTOFMEMORY;
2980 TRACE("Successfully initialized 3D.\n");
2982 return DD_OK;
2985 static HRESULT ddraw_create_gdi_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2987 WINED3DPRESENT_PARAMETERS presentation_parameters;
2988 HWND window;
2989 HRESULT hr;
2991 window = ddraw->dest_window;
2993 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2995 /* Use the surface description for the device parameters, not the device
2996 * settings. The application might render to an offscreen surface. */
2997 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2998 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2999 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3000 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
3001 ? primary->surface_desc.dwBackBufferCount : 0;
3002 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3003 presentation_parameters.MultiSampleQuality = 0;
3004 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
3005 presentation_parameters.hDeviceWindow = window;
3006 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
3007 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
3008 presentation_parameters.AutoDepthStencilFormat = 0;
3009 presentation_parameters.Flags = 0;
3010 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
3011 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3012 presentation_parameters.AutoRestoreDisplayMode = FALSE;
3014 ddraw->d3d_target = primary;
3015 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &presentation_parameters);
3016 ddraw->d3d_target = NULL;
3017 if (FAILED(hr))
3019 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
3020 primary->wined3d_swapchain = NULL;
3023 return hr;
3026 /*****************************************************************************
3027 * IDirectDraw7::CreateSurface
3029 * Creates a new IDirectDrawSurface object and returns its interface.
3031 * The surface connections with wined3d are a bit tricky. Basically it works
3032 * like this:
3034 * |------------------------| |-----------------|
3035 * | DDraw surface | | WineD3DSurface |
3036 * | | | |
3037 * | WineD3DSurface |-------------->| |
3038 * | Child |<------------->| Parent |
3039 * |------------------------| |-----------------|
3041 * The DDraw surface is the parent of the wined3d surface, and it releases
3042 * the WineD3DSurface when the ddraw surface is destroyed.
3044 * However, for all surfaces which can be in a container in WineD3D,
3045 * we have to do this. These surfaces are usually complex surfaces,
3046 * so this concerns primary surfaces with a front and a back buffer,
3047 * and textures.
3049 * |------------------------| |-----------------|
3050 * | DDraw surface | | Container |
3051 * | | | |
3052 * | Child |<------------->| Parent |
3053 * | Texture |<------------->| |
3054 * | WineD3DSurface |<----| | Levels |<--|
3055 * | Complex connection | | | | |
3056 * |------------------------| | |-----------------| |
3057 * ^ | |
3058 * | | |
3059 * | | |
3060 * | |------------------| | |-----------------| |
3061 * | | IParent | |-------->| WineD3DSurface | |
3062 * | | | | | |
3063 * | | Child |<------------->| Parent | |
3064 * | | | | Container |<--|
3065 * | |------------------| |-----------------| |
3066 * | |
3067 * | |----------------------| |
3068 * | | DDraw surface 2 | |
3069 * | | | |
3070 * |<->| Complex root Child | |
3071 * | | Texture | |
3072 * | | WineD3DSurface |<----| |
3073 * | |----------------------| | |
3074 * | | |
3075 * | |---------------------| | |-----------------| |
3076 * | | IParent | |----->| WineD3DSurface | |
3077 * | | | | | |
3078 * | | Child |<---------->| Parent | |
3079 * | |---------------------| | Container |<--|
3080 * | |-----------------| |
3081 * | |
3082 * | ---More surfaces can follow--- |
3084 * The reason is that the IWineD3DSwapchain(render target container)
3085 * and the IWineD3DTexure(Texture container) release the parents
3086 * of their surface's children, but by releasing the complex root
3087 * the surfaces which are complexly attached to it are destroyed
3088 * too. See IDirectDrawSurface::Release for a more detailed
3089 * explanation.
3091 * Params:
3092 * DDSD: Description of the surface to create
3093 * Surf: Address to store the interface pointer at
3094 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
3095 * aggregation, so it has to be NULL
3097 * Returns:
3098 * DD_OK on success
3099 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3100 * DDERR_* if an error occurs
3102 *****************************************************************************/
3103 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
3104 IDirectDrawSurfaceImpl **Surf, IUnknown *UnkOuter)
3106 IDirectDrawSurfaceImpl *object = NULL;
3107 HRESULT hr;
3108 LONG extra_surfaces = 0;
3109 DDSURFACEDESC2 desc2;
3110 WINED3DDISPLAYMODE Mode;
3111 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
3113 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
3115 /* Some checks before we start */
3116 if (TRACE_ON(ddraw))
3118 TRACE(" (%p) Requesting surface desc :\n", ddraw);
3119 DDRAW_dump_surface_desc(DDSD);
3121 EnterCriticalSection(&ddraw_cs);
3123 if (UnkOuter != NULL)
3125 FIXME("(%p) : outer != NULL?\n", ddraw);
3126 LeaveCriticalSection(&ddraw_cs);
3127 return CLASS_E_NOAGGREGATION; /* unchecked */
3130 if (Surf == NULL)
3132 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
3133 LeaveCriticalSection(&ddraw_cs);
3134 return E_POINTER; /* unchecked */
3137 if (!(DDSD->dwFlags & DDSD_CAPS))
3139 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
3140 DDSD->dwFlags |= DDSD_CAPS;
3143 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
3145 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
3146 DDSD->dwFlags &= ~DDSD_LPSURFACE;
3149 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
3151 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
3152 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
3153 DDSD->dwFlags &= ~DDSD_LPSURFACE;
3156 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
3157 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
3159 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
3160 ddraw);
3161 *Surf = NULL;
3162 LeaveCriticalSection(&ddraw_cs);
3163 return DDERR_NOEXCLUSIVEMODE;
3166 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
3168 WARN("Application wanted to create back buffer primary surface\n");
3169 LeaveCriticalSection(&ddraw_cs);
3170 return DDERR_INVALIDCAPS;
3173 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
3175 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
3176 WARN("Application tries to put the surface in both system and video memory\n");
3177 LeaveCriticalSection(&ddraw_cs);
3178 *Surf = NULL;
3179 return DDERR_INVALIDCAPS;
3182 /* Check cube maps but only if the size includes them */
3183 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
3185 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
3186 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
3188 WARN("Cube map faces requested without cube map flag\n");
3189 LeaveCriticalSection(&ddraw_cs);
3190 return DDERR_INVALIDCAPS;
3192 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
3193 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
3195 WARN("Cube map without faces requested\n");
3196 LeaveCriticalSection(&ddraw_cs);
3197 return DDERR_INVALIDPARAMS;
3200 /* Quick tests confirm those can be created, but we don't do that yet */
3201 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
3202 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
3204 FIXME("Partial cube maps not supported yet\n");
3208 /* According to the msdn this flag is ignored by CreateSurface */
3209 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
3210 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
3212 /* Modify some flags */
3213 copy_to_surfacedesc2(&desc2, DDSD);
3214 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
3216 /* Get the video mode from WineD3D - we will need it */
3217 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &Mode);
3218 if (FAILED(hr))
3220 ERR("Failed to read display mode from wined3d\n");
3221 switch(ddraw->orig_bpp)
3223 case 8:
3224 Mode.Format = WINED3DFMT_P8_UINT;
3225 break;
3227 case 15:
3228 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
3229 break;
3231 case 16:
3232 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
3233 break;
3235 case 24:
3236 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
3237 break;
3239 case 32:
3240 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
3241 break;
3243 Mode.Width = ddraw->orig_width;
3244 Mode.Height = ddraw->orig_height;
3247 /* No pixelformat given? Use the current screen format */
3248 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
3250 desc2.dwFlags |= DDSD_PIXELFORMAT;
3251 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
3253 /* Wait: It could be a Z buffer */
3254 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3256 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
3258 case 15:
3259 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
3260 break;
3261 case 16:
3262 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
3263 break;
3264 case 24:
3265 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
3266 break;
3267 case 32:
3268 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
3269 break;
3270 default:
3271 ERR("Unknown Z buffer bit depth\n");
3274 else
3276 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
3280 /* No Width or no Height? Use the original screen size
3282 if(!(desc2.dwFlags & DDSD_WIDTH) ||
3283 !(desc2.dwFlags & DDSD_HEIGHT) )
3285 /* Invalid for non-render targets */
3286 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3288 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
3289 *Surf = NULL;
3290 LeaveCriticalSection(&ddraw_cs);
3291 return DDERR_INVALIDPARAMS;
3294 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
3295 desc2.dwWidth = Mode.Width;
3296 desc2.dwHeight = Mode.Height;
3299 if (!desc2.dwWidth || !desc2.dwHeight)
3301 LeaveCriticalSection(&ddraw_cs);
3302 return DDERR_INVALIDPARAMS;
3305 /* Mipmap count fixes */
3306 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3308 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
3310 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
3312 /* Mipmap count is given, should not be 0 */
3313 if( desc2.u2.dwMipMapCount == 0 )
3315 LeaveCriticalSection(&ddraw_cs);
3316 return DDERR_INVALIDPARAMS;
3319 else
3321 /* Undocumented feature: Create sublevels until
3322 * either the width or the height is 1
3324 DWORD min = desc2.dwWidth < desc2.dwHeight ?
3325 desc2.dwWidth : desc2.dwHeight;
3326 desc2.u2.dwMipMapCount = 0;
3327 while( min )
3329 desc2.u2.dwMipMapCount += 1;
3330 min >>= 1;
3334 else
3336 /* Not-complex mipmap -> Mipmapcount = 1 */
3337 desc2.u2.dwMipMapCount = 1;
3339 extra_surfaces = desc2.u2.dwMipMapCount - 1;
3341 /* There's a mipmap count in the created surface in any case */
3342 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
3344 /* If no mipmap is given, the texture has only one level */
3346 /* The first surface is a front buffer, the back buffer is created afterwards */
3347 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
3349 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
3352 /* The root surface in a cube map is positive x */
3353 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3355 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3356 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3359 /* Create the first surface */
3360 hr = ddraw_create_surface(ddraw, &desc2, &object, 0);
3361 if (FAILED(hr))
3363 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
3364 LeaveCriticalSection(&ddraw_cs);
3365 return hr;
3367 object->is_complex_root = TRUE;
3369 *Surf = object;
3371 /* Create Additional surfaces if necessary
3372 * This applies to Primary surfaces which have a back buffer count
3373 * set, but not to mipmap textures. In case of Mipmap textures,
3374 * wineD3D takes care of the creation of additional surfaces
3376 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
3378 extra_surfaces = DDSD->dwBackBufferCount;
3379 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
3380 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
3381 desc2.dwBackBufferCount = 0;
3384 hr = DD_OK;
3385 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3387 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3388 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
3389 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3390 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
3391 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
3392 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3393 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
3394 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
3395 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3396 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
3397 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
3398 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3399 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
3400 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
3401 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3402 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
3403 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3406 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE);
3407 if(hr != DD_OK)
3409 /* This destroys and possibly created surfaces too */
3410 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
3411 LeaveCriticalSection(&ddraw_cs);
3412 return hr;
3415 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
3416 * But attach the d3ddevice only if the currently created surface was
3417 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
3418 * The only case I can think of where this doesn't apply is when a
3419 * 2D app was configured by the user to run with OpenGL and it didn't create
3420 * the render target as first surface. In this case the render target creation
3421 * will cause the 3D init.
3423 if( (ddraw->ImplType == SURFACE_OPENGL) && !(ddraw->d3d_initialized) &&
3424 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
3426 IDirectDrawSurfaceImpl *target = object, *surface;
3427 struct list *entry;
3429 /* Search for the primary to use as render target */
3430 LIST_FOR_EACH(entry, &ddraw->surface_list)
3432 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3433 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
3434 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
3436 /* found */
3437 target = surface;
3438 TRACE("Using primary %p as render target\n", target);
3439 break;
3443 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", ddraw, target);
3444 hr = ddraw_attach_d3d_device(ddraw, target);
3445 if (hr != D3D_OK)
3447 IDirectDrawSurfaceImpl *release_surf;
3448 ERR("ddraw_attach_d3d_device failed, hr %#x\n", hr);
3449 *Surf = NULL;
3451 /* The before created surface structures are in an incomplete state here.
3452 * WineD3D holds the reference on the IParents, and it released them on the failure
3453 * already. So the regular release method implementation would fail on the attempt
3454 * to destroy either the IParents or the swapchain. So free the surface here.
3455 * The surface structure here is a list, not a tree, because onscreen targets
3456 * cannot be cube textures
3458 while(object)
3460 release_surf = object;
3461 object = object->complex_array[0];
3462 ddraw_surface_destroy(release_surf);
3464 LeaveCriticalSection(&ddraw_cs);
3465 return hr;
3468 else if(!(ddraw->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3470 ddraw_create_gdi_swapchain(ddraw, object);
3473 /* Addref the ddraw interface to keep an reference for each surface */
3474 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
3475 object->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
3477 /* Create a WineD3DTexture if a texture was requested */
3478 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3480 ddraw->tex_root = object;
3481 ddraw_surface_create_texture(object);
3482 ddraw->tex_root = NULL;
3485 LeaveCriticalSection(&ddraw_cs);
3486 return hr;
3489 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
3490 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3492 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3493 IDirectDrawSurfaceImpl *impl;
3494 HRESULT hr;
3496 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3497 iface, surface_desc, surface, outer_unknown);
3499 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3501 WARN("Application supplied invalid surface descriptor\n");
3502 return DDERR_INVALIDPARAMS;
3505 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3507 if (TRACE_ON(ddraw))
3509 TRACE(" (%p) Requesting surface desc :\n", iface);
3510 DDRAW_dump_surface_desc(surface_desc);
3513 WARN("Application tried to create an explicit front or back buffer\n");
3514 return DDERR_INVALIDCAPS;
3517 hr = CreateSurface(This, surface_desc, &impl, outer_unknown);
3518 if (FAILED(hr))
3520 *surface = NULL;
3521 return hr;
3524 *surface = &impl->IDirectDrawSurface7_iface;
3526 return hr;
3529 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3530 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3532 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3533 IDirectDrawSurfaceImpl *impl;
3534 HRESULT hr;
3536 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3537 iface, surface_desc, surface, outer_unknown);
3539 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3541 WARN("Application supplied invalid surface descriptor\n");
3542 return DDERR_INVALIDPARAMS;
3545 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3547 if (TRACE_ON(ddraw))
3549 TRACE(" (%p) Requesting surface desc :\n", iface);
3550 DDRAW_dump_surface_desc(surface_desc);
3553 WARN("Application tried to create an explicit front or back buffer\n");
3554 return DDERR_INVALIDCAPS;
3557 hr = CreateSurface(This, surface_desc, &impl, outer_unknown);
3558 if (FAILED(hr))
3560 *surface = NULL;
3561 return hr;
3564 *surface = &impl->IDirectDrawSurface4_iface;
3565 ddraw_set_surface_version(impl, 4);
3566 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3567 IDirectDraw4_AddRef(iface);
3568 impl->ifaceToRelease = (IUnknown *)iface;
3570 return hr;
3573 static HRESULT WINAPI ddraw3_CreateSurface(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc,
3574 IDirectDrawSurface **surface, IUnknown *outer_unknown)
3576 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3577 IDirectDrawSurfaceImpl *impl;
3578 HRESULT hr;
3580 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3581 iface, surface_desc, surface, outer_unknown);
3583 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3585 WARN("Application supplied invalid surface descriptor\n");
3586 return DDERR_INVALIDPARAMS;
3589 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3591 if (TRACE_ON(ddraw))
3593 TRACE(" (%p) Requesting surface desc :\n", iface);
3594 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3597 WARN("Application tried to create an explicit front or back buffer\n");
3598 return DDERR_INVALIDCAPS;
3601 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &impl, outer_unknown);
3602 if (FAILED(hr))
3604 *surface = NULL;
3605 return hr;
3608 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_iface;
3609 ddraw_set_surface_version(impl, 3);
3610 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3611 IDirectDraw3_AddRef(iface);
3612 impl->ifaceToRelease = (IUnknown *)iface;
3614 return hr;
3617 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3618 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3620 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3621 IDirectDrawSurfaceImpl *impl;
3622 HRESULT hr;
3624 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3625 iface, surface_desc, surface, outer_unknown);
3627 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3629 WARN("Application supplied invalid surface descriptor\n");
3630 return DDERR_INVALIDPARAMS;
3633 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3635 if (TRACE_ON(ddraw))
3637 TRACE(" (%p) Requesting surface desc :\n", iface);
3638 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3641 WARN("Application tried to create an explicit front or back buffer\n");
3642 return DDERR_INVALIDCAPS;
3645 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &impl, outer_unknown);
3646 if (FAILED(hr))
3648 *surface = NULL;
3649 return hr;
3652 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_iface;
3653 ddraw_set_surface_version(impl, 2);
3654 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3655 impl->ifaceToRelease = NULL;
3657 return hr;
3660 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3661 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3663 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3664 IDirectDrawSurfaceImpl *impl;
3665 HRESULT hr;
3667 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3668 iface, surface_desc, surface, outer_unknown);
3670 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3672 WARN("Application supplied invalid surface descriptor\n");
3673 return DDERR_INVALIDPARAMS;
3676 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3677 * primaries anyway. */
3678 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3679 hr = CreateSurface(This, (DDSURFACEDESC2 *)surface_desc, &impl, outer_unknown);
3680 if (FAILED(hr))
3682 *surface = NULL;
3683 return hr;
3686 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_iface;
3687 ddraw_set_surface_version(impl, 1);
3688 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3689 impl->ifaceToRelease = NULL;
3691 return hr;
3694 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3695 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3697 static BOOL
3698 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3699 const DDPIXELFORMAT *provided)
3701 /* Some flags must be present in both or neither for a match. */
3702 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3703 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3704 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3706 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3707 return FALSE;
3709 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3710 return FALSE;
3712 if (requested->dwFlags & DDPF_FOURCC)
3713 if (requested->dwFourCC != provided->dwFourCC)
3714 return FALSE;
3716 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3717 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3718 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3719 return FALSE;
3721 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3722 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3723 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3724 return FALSE;
3726 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3727 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3728 return FALSE;
3730 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3731 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3732 |DDPF_BUMPDUDV))
3733 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3734 return FALSE;
3736 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3737 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3738 return FALSE;
3740 return TRUE;
3743 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3745 struct compare_info
3747 DWORD flag;
3748 ptrdiff_t offset;
3749 size_t size;
3752 #define CMP(FLAG, FIELD) \
3753 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3754 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3756 static const struct compare_info compare[] =
3758 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3759 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3760 CMP(CAPS, ddsCaps),
3761 CMP(CKDESTBLT, ddckCKDestBlt),
3762 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3763 CMP(CKSRCBLT, ddckCKSrcBlt),
3764 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3765 CMP(HEIGHT, dwHeight),
3766 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3767 CMP(LPSURFACE, lpSurface),
3768 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3769 CMP(PITCH, u1 /* lPitch */),
3770 /* PIXELFORMAT: manual */
3771 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3772 CMP(TEXTURESTAGE, dwTextureStage),
3773 CMP(WIDTH, dwWidth),
3774 /* ZBUFFERBITDEPTH: "obsolete" */
3777 #undef CMP
3779 unsigned int i;
3781 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3782 return FALSE;
3784 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3786 if (requested->dwFlags & compare[i].flag
3787 && memcmp((const char *)provided + compare[i].offset,
3788 (const char *)requested + compare[i].offset,
3789 compare[i].size) != 0)
3790 return FALSE;
3793 if (requested->dwFlags & DDSD_PIXELFORMAT)
3795 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3796 &provided->u4.ddpfPixelFormat))
3797 return FALSE;
3800 return TRUE;
3803 #undef DDENUMSURFACES_SEARCHTYPE
3804 #undef DDENUMSURFACES_MATCHTYPE
3806 struct surfacescallback_context
3808 LPDDENUMSURFACESCALLBACK func;
3809 void *context;
3812 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3813 DDSURFACEDESC2 *surface_desc, void *context)
3815 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3816 struct surfacescallback_context *cbcontext = context;
3818 return cbcontext->func((IDirectDrawSurface *)&surface_impl->IDirectDrawSurface3_iface,
3819 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3822 /*****************************************************************************
3823 * IDirectDraw7::EnumSurfaces
3825 * Loops through all surfaces attached to this device and calls the
3826 * application callback. This can't be relayed to WineD3DDevice,
3827 * because some WineD3DSurfaces' parents are IParent objects
3829 * Params:
3830 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3831 * DDSD: Description to filter for
3832 * Context: Application-provided pointer, it's passed unmodified to the
3833 * Callback function
3834 * Callback: Address to call for each surface
3836 * Returns:
3837 * DDERR_INVALIDPARAMS if the callback is NULL
3838 * DD_OK on success
3840 *****************************************************************************/
3841 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3842 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3844 /* The surface enumeration is handled by WineDDraw,
3845 * because it keeps track of all surfaces attached to
3846 * it. The filtering is done by our callback function,
3847 * because WineDDraw doesn't handle ddraw-like surface
3848 * caps structures
3850 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3851 IDirectDrawSurfaceImpl *surf;
3852 BOOL all, nomatch;
3853 DDSURFACEDESC2 desc;
3854 struct list *entry, *entry2;
3856 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3857 iface, Flags, DDSD, Context, Callback);
3859 all = Flags & DDENUMSURFACES_ALL;
3860 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3862 EnterCriticalSection(&ddraw_cs);
3864 if(!Callback)
3866 LeaveCriticalSection(&ddraw_cs);
3867 return DDERR_INVALIDPARAMS;
3870 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3871 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3873 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3874 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3876 desc = surf->surface_desc;
3877 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3878 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3880 LeaveCriticalSection(&ddraw_cs);
3881 return DD_OK;
3885 LeaveCriticalSection(&ddraw_cs);
3886 return DD_OK;
3889 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3890 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3892 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3894 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3895 iface, flags, surface_desc, context, callback);
3897 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, surface_desc, context,
3898 (LPDDENUMSURFACESCALLBACK7)callback);
3901 static HRESULT WINAPI ddraw3_EnumSurfaces(IDirectDraw3 *iface, DWORD flags,
3902 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3904 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
3905 struct surfacescallback_context cbcontext;
3907 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3908 iface, flags, surface_desc, context, callback);
3910 cbcontext.func = callback;
3911 cbcontext.context = context;
3913 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3914 &cbcontext, EnumSurfacesCallbackThunk);
3917 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3918 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3920 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3921 struct surfacescallback_context cbcontext;
3923 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3924 iface, flags, surface_desc, context, callback);
3926 cbcontext.func = callback;
3927 cbcontext.context = context;
3929 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3930 &cbcontext, EnumSurfacesCallbackThunk);
3933 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3934 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3936 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3937 struct surfacescallback_context cbcontext;
3939 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3940 iface, flags, surface_desc, context, callback);
3942 cbcontext.func = callback;
3943 cbcontext.context = context;
3945 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, (DDSURFACEDESC2 *)surface_desc,
3946 &cbcontext, EnumSurfacesCallbackThunk);
3949 /*****************************************************************************
3950 * DirectDrawCreateClipper (DDRAW.@)
3952 * Creates a new IDirectDrawClipper object.
3954 * Params:
3955 * Clipper: Address to write the interface pointer to
3956 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3957 * NULL
3959 * Returns:
3960 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3961 * E_OUTOFMEMORY if allocating the object failed
3963 *****************************************************************************/
3964 HRESULT WINAPI
3965 DirectDrawCreateClipper(DWORD Flags,
3966 LPDIRECTDRAWCLIPPER *Clipper,
3967 IUnknown *UnkOuter)
3969 IDirectDrawClipperImpl* object;
3970 HRESULT hr;
3972 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3973 Flags, Clipper, UnkOuter);
3975 EnterCriticalSection(&ddraw_cs);
3976 if (UnkOuter != NULL)
3978 LeaveCriticalSection(&ddraw_cs);
3979 return CLASS_E_NOAGGREGATION;
3982 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3983 sizeof(IDirectDrawClipperImpl));
3984 if (object == NULL)
3986 LeaveCriticalSection(&ddraw_cs);
3987 return E_OUTOFMEMORY;
3990 hr = ddraw_clipper_init(object);
3991 if (FAILED(hr))
3993 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3994 HeapFree(GetProcessHeap(), 0, object);
3995 LeaveCriticalSection(&ddraw_cs);
3996 return hr;
3999 TRACE("Created clipper %p.\n", object);
4000 *Clipper = (IDirectDrawClipper *) object;
4001 LeaveCriticalSection(&ddraw_cs);
4002 return DD_OK;
4005 /*****************************************************************************
4006 * IDirectDraw7::CreateClipper
4008 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
4010 *****************************************************************************/
4011 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
4012 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
4014 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
4015 iface, Flags, Clipper, UnkOuter);
4017 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
4020 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
4021 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
4023 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4025 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
4026 iface, flags, clipper, outer_unknown);
4028 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
4031 static HRESULT WINAPI ddraw3_CreateClipper(IDirectDraw3 *iface, DWORD flags,
4032 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
4034 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4036 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
4037 iface, flags, clipper, outer_unknown);
4039 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
4042 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
4043 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
4045 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4047 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
4048 iface, flags, clipper, outer_unknown);
4050 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
4053 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
4054 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
4056 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4058 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
4059 iface, flags, clipper, outer_unknown);
4061 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
4064 /*****************************************************************************
4065 * IDirectDraw7::CreatePalette
4067 * Creates a new IDirectDrawPalette object
4069 * Params:
4070 * Flags: The flags for the new clipper
4071 * ColorTable: Color table to assign to the new clipper
4072 * Palette: Address to write the interface pointer to
4073 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
4074 * NULL
4076 * Returns:
4077 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
4078 * E_OUTOFMEMORY if allocating the object failed
4080 *****************************************************************************/
4081 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
4082 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
4084 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
4085 IDirectDrawPaletteImpl *object;
4086 HRESULT hr;
4088 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
4089 iface, Flags, ColorTable, Palette, pUnkOuter);
4091 EnterCriticalSection(&ddraw_cs);
4092 if(pUnkOuter != NULL)
4094 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
4095 LeaveCriticalSection(&ddraw_cs);
4096 return CLASS_E_NOAGGREGATION;
4099 /* The refcount test shows that a cooplevel is required for this */
4100 if(!This->cooperative_level)
4102 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
4103 LeaveCriticalSection(&ddraw_cs);
4104 return DDERR_NOCOOPERATIVELEVELSET;
4107 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
4108 if(!object)
4110 ERR("Out of memory when allocating memory for a palette implementation\n");
4111 LeaveCriticalSection(&ddraw_cs);
4112 return E_OUTOFMEMORY;
4115 hr = ddraw_palette_init(object, This, Flags, ColorTable);
4116 if (FAILED(hr))
4118 WARN("Failed to initialize palette, hr %#x.\n", hr);
4119 HeapFree(GetProcessHeap(), 0, object);
4120 LeaveCriticalSection(&ddraw_cs);
4121 return hr;
4124 TRACE("Created palette %p.\n", object);
4125 *Palette = (IDirectDrawPalette *)object;
4126 LeaveCriticalSection(&ddraw_cs);
4127 return DD_OK;
4130 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
4131 IDirectDrawPalette **palette, IUnknown *outer_unknown)
4133 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4134 HRESULT hr;
4136 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4137 iface, flags, entries, palette, outer_unknown);
4139 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4140 if (SUCCEEDED(hr) && *palette)
4142 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4143 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4144 IDirectDraw4_AddRef(iface);
4145 impl->ifaceToRelease = (IUnknown *)iface;
4147 return hr;
4150 static HRESULT WINAPI ddraw3_CreatePalette(IDirectDraw3 *iface, DWORD flags,
4151 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4153 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4154 HRESULT hr;
4156 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4157 iface, flags, entries, palette, outer_unknown);
4159 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4160 if (SUCCEEDED(hr) && *palette)
4162 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4163 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4164 IDirectDraw4_AddRef(iface);
4165 impl->ifaceToRelease = (IUnknown *)iface;
4168 return hr;
4171 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
4172 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4174 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4175 HRESULT hr;
4177 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4178 iface, flags, entries, palette, outer_unknown);
4180 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4181 if (SUCCEEDED(hr) && *palette)
4183 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4184 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4185 impl->ifaceToRelease = NULL;
4188 return hr;
4191 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
4192 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4194 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4195 HRESULT hr;
4197 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4198 iface, flags, entries, palette, outer_unknown);
4200 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
4201 if (SUCCEEDED(hr) && *palette)
4203 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4204 IDirectDraw7_Release(&This->IDirectDraw7_iface);
4205 impl->ifaceToRelease = NULL;
4208 return hr;
4211 /*****************************************************************************
4212 * IDirectDraw7::DuplicateSurface
4214 * Duplicates a surface. The surface memory points to the same memory as
4215 * the original surface, and it's released when the last surface referencing
4216 * it is released. I guess that's beyond Wine's surface management right now
4217 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
4218 * test application to implement this)
4220 * Params:
4221 * Src: Address of the source surface
4222 * Dest: Address to write the new surface pointer to
4224 * Returns:
4225 * See IDirectDraw7::CreateSurface
4227 *****************************************************************************/
4228 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
4229 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
4231 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Src);
4233 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
4235 /* For now, simply create a new, independent surface */
4236 return IDirectDraw7_CreateSurface(iface,
4237 &Surf->surface_desc,
4238 Dest,
4239 NULL);
4242 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
4243 IDirectDrawSurface4 **dst)
4245 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
4246 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
4247 IDirectDrawSurface7 *dst7;
4248 IDirectDrawSurfaceImpl *dst_impl;
4249 HRESULT hr;
4251 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4252 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
4253 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
4254 if (FAILED(hr))
4256 *dst = NULL;
4257 return hr;
4259 dst_impl = impl_from_IDirectDrawSurface7(dst7);
4260 *dst = &dst_impl->IDirectDrawSurface4_iface;
4262 return hr;
4265 static HRESULT WINAPI ddraw3_DuplicateSurface(IDirectDraw3 *iface, IDirectDrawSurface *src,
4266 IDirectDrawSurface **dst)
4268 IDirectDrawImpl *This = impl_from_IDirectDraw3(iface);
4269 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)src);
4270 IDirectDrawSurface7 *dst7;
4271 IDirectDrawSurfaceImpl *dst_impl;
4272 HRESULT hr;
4274 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4275 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
4276 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
4277 if (FAILED(hr))
4278 return hr;
4279 dst_impl = impl_from_IDirectDrawSurface7(dst7);
4280 *dst = (IDirectDrawSurface *)&dst_impl->IDirectDrawSurface3_iface;
4282 return hr;
4285 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
4286 IDirectDrawSurface *src, IDirectDrawSurface **dst)
4288 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
4289 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)src);
4290 IDirectDrawSurface7 *dst7;
4291 IDirectDrawSurfaceImpl *dst_impl;
4292 HRESULT hr;
4294 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4295 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
4296 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
4297 if (FAILED(hr))
4298 return hr;
4299 dst_impl = impl_from_IDirectDrawSurface7(dst7);
4300 *dst = (IDirectDrawSurface *)&dst_impl->IDirectDrawSurface3_iface;
4302 return hr;
4305 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
4306 IDirectDrawSurface **dst)
4308 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
4309 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)src);
4310 IDirectDrawSurface7 *dst7;
4311 IDirectDrawSurfaceImpl *dst_impl;
4312 HRESULT hr;
4314 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4315 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
4316 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
4317 if (FAILED(hr))
4318 return hr;
4319 dst_impl = impl_from_IDirectDrawSurface7(dst7);
4320 *dst = (IDirectDrawSurface *)&dst_impl->IDirectDrawSurface3_iface;
4322 return hr;
4325 /*****************************************************************************
4326 * IDirect3D7::EnumDevices
4328 * The EnumDevices method for IDirect3D7. It enumerates all supported
4329 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
4331 * Params:
4332 * callback: Function to call for each enumerated device
4333 * context: Pointer to pass back to the app
4335 * Returns:
4336 * D3D_OK, or the return value of the GetCaps call
4338 *****************************************************************************/
4339 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
4341 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4342 D3DDEVICEDESC7 device_desc7;
4343 D3DDEVICEDESC device_desc1;
4344 HRESULT hr;
4345 size_t i;
4347 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4349 if (!callback)
4350 return DDERR_INVALIDPARAMS;
4352 EnterCriticalSection(&ddraw_cs);
4354 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
4355 if (hr != D3D_OK)
4357 LeaveCriticalSection(&ddraw_cs);
4358 return hr;
4361 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
4363 HRESULT ret;
4365 device_desc7.deviceGUID = *device_list7[i].device_guid;
4366 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
4367 if (ret != DDENUMRET_OK)
4369 TRACE("Application cancelled the enumeration.\n");
4370 LeaveCriticalSection(&ddraw_cs);
4371 return D3D_OK;
4375 TRACE("End of enumeration.\n");
4377 LeaveCriticalSection(&ddraw_cs);
4379 return D3D_OK;
4382 /*****************************************************************************
4383 * IDirect3D3::EnumDevices
4385 * Enumerates all supported Direct3DDevice interfaces. This is the
4386 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
4388 * Version 1, 2 and 3
4390 * Params:
4391 * callback: Application-provided routine to call for each enumerated device
4392 * Context: Pointer to pass to the callback
4394 * Returns:
4395 * D3D_OK on success,
4396 * The result of IDirect3DImpl_GetCaps if it failed
4398 *****************************************************************************/
4399 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4401 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
4403 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4404 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
4405 D3DDEVICEDESC7 device_desc7;
4406 HRESULT hr;
4408 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
4409 * name string. Let's put the string in a sufficiently sized array in
4410 * writable memory. */
4411 char device_name[50];
4412 strcpy(device_name,"Direct3D HEL");
4414 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4416 if (!callback)
4417 return DDERR_INVALIDPARAMS;
4419 EnterCriticalSection(&ddraw_cs);
4421 hr = IDirect3DImpl_GetCaps(This->wineD3D, &device_desc1, &device_desc7);
4422 if (hr != D3D_OK)
4424 LeaveCriticalSection(&ddraw_cs);
4425 return hr;
4428 /* Do I have to enumerate the reference id? Note from old d3d7:
4429 * "It seems that enumerating the reference IID on Direct3D 1 games
4430 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
4432 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
4433 * EnumReference which enables / disables enumerating the reference
4434 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
4435 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
4436 * demo directory suggest this.
4438 * Some games(GTA 2) seem to use the second enumerated device, so I have
4439 * to enumerate at least 2 devices. So enumerate the reference device to
4440 * have 2 devices.
4442 * Other games (Rollcage) tell emulation and hal device apart by certain
4443 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
4444 * limitation flag), and it refuses all devices that have the perspective
4445 * flag set. This way it refuses the emulation device, and HAL devices
4446 * never have POW2 unset in d3d7 on windows. */
4447 if (This->d3dversion != 1)
4449 static CHAR reference_description[] = "RGB Direct3D emulation";
4451 TRACE("Enumerating WineD3D D3DDevice interface.\n");
4452 hal_desc = device_desc1;
4453 hel_desc = device_desc1;
4454 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
4455 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4456 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4457 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4458 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4459 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
4460 device_name, &hal_desc, &hel_desc, context);
4461 if (hr != D3DENUMRET_OK)
4463 TRACE("Application cancelled the enumeration.\n");
4464 LeaveCriticalSection(&ddraw_cs);
4465 return D3D_OK;
4469 strcpy(device_name,"Direct3D HAL");
4471 TRACE("Enumerating HAL Direct3D device.\n");
4472 hal_desc = device_desc1;
4473 hel_desc = device_desc1;
4474 /* The hal device does not have the pow2 flag set in hel, but in hal. */
4475 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4476 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4477 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4478 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4479 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
4480 device_name, &hal_desc, &hel_desc, context);
4481 if (hr != D3DENUMRET_OK)
4483 TRACE("Application cancelled the enumeration.\n");
4484 LeaveCriticalSection(&ddraw_cs);
4485 return D3D_OK;
4488 TRACE("End of enumeration.\n");
4490 LeaveCriticalSection(&ddraw_cs);
4491 return D3D_OK;
4494 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4496 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4498 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4500 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4503 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4505 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4507 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4509 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4512 /*****************************************************************************
4513 * IDirect3D3::CreateLight
4515 * Creates an IDirect3DLight interface. This interface is used in
4516 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4517 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4518 * uses the IDirect3DDevice7 interface with D3D7 lights.
4520 * Version 1, 2 and 3
4522 * Params:
4523 * light: Address to store the new interface pointer
4524 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4525 * Must be NULL
4527 * Returns:
4528 * D3D_OK on success
4529 * DDERR_OUTOFMEMORY if memory allocation failed
4530 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4532 *****************************************************************************/
4533 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
4534 IUnknown *outer_unknown)
4536 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4537 IDirect3DLightImpl *object;
4539 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4541 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4543 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4544 if (!object)
4546 ERR("Failed to allocate light memory.\n");
4547 return DDERR_OUTOFMEMORY;
4550 d3d_light_init(object, This);
4552 TRACE("Created light %p.\n", object);
4553 *light = (IDirect3DLight *)object;
4555 return D3D_OK;
4558 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4560 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4562 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4564 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4567 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4569 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4571 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4573 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4576 /*****************************************************************************
4577 * IDirect3D3::CreateMaterial
4579 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4580 * and older versions. The IDirect3DMaterial implementation wraps its
4581 * functionality to IDirect3DDevice7::SetMaterial and friends.
4583 * Version 1, 2 and 3
4585 * Params:
4586 * material: Address to store the new interface's pointer to
4587 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4588 * Must be NULL
4590 * Returns:
4591 * D3D_OK on success
4592 * DDERR_OUTOFMEMORY if memory allocation failed
4593 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4595 *****************************************************************************/
4596 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4597 IUnknown *outer_unknown)
4599 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4600 IDirect3DMaterialImpl *object;
4602 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4604 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4606 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4607 if (!object)
4609 ERR("Failed to allocate material memory.\n");
4610 return DDERR_OUTOFMEMORY;
4613 d3d_material_init(object, This);
4615 TRACE("Created material %p.\n", object);
4616 *material = (IDirect3DMaterial3 *)object;
4618 return D3D_OK;
4621 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material, IUnknown *outer_unknown)
4623 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4624 IDirect3DMaterial3 *material3;
4625 HRESULT hr;
4627 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4629 hr = d3d3_CreateMaterial(&This->IDirect3D3_iface, &material3, outer_unknown);
4630 *material = material3 ? &((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial2_iface : NULL;
4632 TRACE("Returning material %p.\n", *material);
4634 return hr;
4637 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material, IUnknown *outer_unknown)
4639 IDirect3DMaterial3 *material3;
4640 HRESULT hr;
4642 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4644 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4646 hr = d3d3_CreateMaterial(&This->IDirect3D3_iface, &material3, outer_unknown);
4647 *material = material3 ? &((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial_iface : NULL;
4649 TRACE("Returning material %p.\n", *material);
4651 return hr;
4654 /*****************************************************************************
4655 * IDirect3D3::CreateViewport
4657 * Creates an IDirect3DViewport interface. This interface is used
4658 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4659 * it has been replaced by a viewport structure and
4660 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4661 * uses the IDirect3DDevice7 methods for its functionality
4663 * Params:
4664 * Viewport: Address to store the new interface pointer
4665 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4666 * Must be NULL
4668 * Returns:
4669 * D3D_OK on success
4670 * DDERR_OUTOFMEMORY if memory allocation failed
4671 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4673 *****************************************************************************/
4674 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4675 IUnknown *outer_unknown)
4677 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4678 IDirect3DViewportImpl *object;
4680 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4682 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4684 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4685 if (!object)
4687 ERR("Failed to allocate viewport memory.\n");
4688 return DDERR_OUTOFMEMORY;
4691 d3d_viewport_init(object, This);
4693 TRACE("Created viewport %p.\n", object);
4694 *viewport = (IDirect3DViewport3 *)object;
4696 return D3D_OK;
4699 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4701 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4703 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4705 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4706 outer_unknown);
4709 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4711 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4713 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4715 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4716 outer_unknown);
4719 /*****************************************************************************
4720 * IDirect3D3::FindDevice
4722 * This method finds a device with the requested properties and returns a
4723 * device description
4725 * Verion 1, 2 and 3
4726 * Params:
4727 * fds: Describes the requested device characteristics
4728 * fdr: Returns the device description
4730 * Returns:
4731 * D3D_OK on success
4732 * DDERR_INVALIDPARAMS if no device was found
4734 *****************************************************************************/
4735 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4737 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4738 D3DDEVICEDESC7 desc7;
4739 D3DDEVICEDESC desc1;
4740 HRESULT hr;
4742 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4744 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4746 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4747 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4748 return DDERR_INVALIDPARAMS;
4750 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4751 && fds->dcmColorModel != D3DCOLOR_RGB)
4753 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4754 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4757 if (fds->dwFlags & D3DFDS_GUID)
4759 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4760 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4761 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4762 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4764 WARN("No match for this GUID.\n");
4765 return DDERR_NOTFOUND;
4769 /* Get the caps */
4770 hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc1, &desc7);
4771 if (hr != D3D_OK) return hr;
4773 /* Now return our own GUID */
4774 fdr->guid = IID_D3DDEVICE_WineD3D;
4775 fdr->ddHwDesc = desc1;
4776 fdr->ddSwDesc = desc1;
4778 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4780 return D3D_OK;
4783 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4785 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4787 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4789 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4792 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4794 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4796 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4798 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4801 /*****************************************************************************
4802 * IDirect3D7::CreateDevice
4804 * Creates an IDirect3DDevice7 interface.
4806 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4807 * DirectDraw surfaces and are created with
4808 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4809 * create the device object and QueryInterfaces for IDirect3DDevice
4811 * Params:
4812 * refiid: IID of the device to create
4813 * Surface: Initial rendertarget
4814 * Device: Address to return the interface pointer
4816 * Returns:
4817 * D3D_OK on success
4818 * DDERR_OUTOFMEMORY if memory allocation failed
4819 * DDERR_INVALIDPARAMS if a device exists already
4821 *****************************************************************************/
4822 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4823 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4825 IDirectDrawSurfaceImpl *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4826 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4827 IDirect3DDeviceImpl *object;
4828 HRESULT hr;
4830 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4832 EnterCriticalSection(&ddraw_cs);
4833 *device = NULL;
4835 /* Fail device creation if non-opengl surfaces are used. */
4836 if (This->ImplType != SURFACE_OPENGL)
4838 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4839 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4841 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4842 * is caught in CreateSurface or QueryInterface. */
4843 LeaveCriticalSection(&ddraw_cs);
4844 return DDERR_NO3D;
4847 if (This->d3ddevice)
4849 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4850 LeaveCriticalSection(&ddraw_cs);
4851 return DDERR_INVALIDPARAMS;
4854 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4855 if (!object)
4857 ERR("Failed to allocate device memory.\n");
4858 LeaveCriticalSection(&ddraw_cs);
4859 return DDERR_OUTOFMEMORY;
4862 hr = d3d_device_init(object, This, target);
4863 if (FAILED(hr))
4865 WARN("Failed to initialize device, hr %#x.\n", hr);
4866 HeapFree(GetProcessHeap(), 0, object);
4867 LeaveCriticalSection(&ddraw_cs);
4868 return hr;
4871 TRACE("Created device %p.\n", object);
4872 *device = (IDirect3DDevice7 *)object;
4874 LeaveCriticalSection(&ddraw_cs);
4875 return D3D_OK;
4878 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4879 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4881 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4882 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4883 HRESULT hr;
4885 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4886 iface, debugstr_guid(riid), surface, device, outer_unknown);
4888 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4890 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4891 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL,
4892 (IDirect3DDevice7 **)device);
4893 if (*device) *device = (IDirect3DDevice3 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice3_vtbl;
4895 return hr;
4898 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4899 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4901 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4902 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface3((IDirectDrawSurface3 *)surface);
4903 HRESULT hr;
4905 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4906 iface, debugstr_guid(riid), surface, device);
4908 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4909 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL,
4910 (IDirect3DDevice7 **)device);
4911 if (*device) *device = (IDirect3DDevice2 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice2_vtbl;
4913 return hr;
4916 /*****************************************************************************
4917 * IDirect3D7::CreateVertexBuffer
4919 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4920 * interface.
4922 * Version 3 and 7
4924 * Params:
4925 * desc: Requested Vertex buffer properties
4926 * vertex_buffer: Address to return the interface pointer at
4927 * flags: Some flags, should be 0
4929 * Returns
4930 * D3D_OK on success
4931 * DDERR_OUTOFMEMORY if memory allocation failed
4932 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4933 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4935 *****************************************************************************/
4936 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4937 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4939 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4940 IDirect3DVertexBufferImpl *object;
4941 HRESULT hr;
4943 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4944 iface, desc, vertex_buffer, flags);
4946 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4948 TRACE("Vertex buffer description:\n");
4949 TRACE(" dwSize %u\n", desc->dwSize);
4950 TRACE(" dwCaps %#x\n", desc->dwCaps);
4951 TRACE(" FVF %#x\n", desc->dwFVF);
4952 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
4954 /* Now create the vertex buffer */
4955 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4956 if (!object)
4958 ERR("Failed to allocate vertex buffer memory.\n");
4959 return DDERR_OUTOFMEMORY;
4962 hr = d3d_vertex_buffer_init(object, This, desc);
4963 if (FAILED(hr))
4965 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
4966 HeapFree(GetProcessHeap(), 0, object);
4967 return hr;
4970 TRACE("Created vertex buffer %p.\n", object);
4971 *vertex_buffer = (IDirect3DVertexBuffer7 *)object;
4973 return D3D_OK;
4976 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4977 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4979 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4980 HRESULT hr;
4982 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4983 iface, desc, vertex_buffer, flags, outer_unknown);
4985 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4987 hr = d3d7_CreateVertexBuffer(&This->IDirect3D7_iface, desc,
4988 (IDirect3DVertexBuffer7 **)vertex_buffer, flags);
4989 if (*vertex_buffer)
4990 *vertex_buffer = (IDirect3DVertexBuffer *)&((IDirect3DVertexBufferImpl *)*vertex_buffer)->IDirect3DVertexBuffer_vtbl;
4992 return hr;
4995 /*****************************************************************************
4996 * IDirect3D7::EnumZBufferFormats
4998 * Enumerates all supported Z buffer pixel formats
5000 * Version 3 and 7
5002 * Params:
5003 * device_iid:
5004 * callback: callback to call for each pixel format
5005 * context: Pointer to pass back to the callback
5007 * Returns:
5008 * D3D_OK on success
5009 * DDERR_INVALIDPARAMS if callback is NULL
5010 * For details, see IWineD3DDevice::EnumZBufferFormats
5012 *****************************************************************************/
5013 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
5014 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
5016 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
5017 WINED3DDISPLAYMODE d3ddm;
5018 WINED3DDEVTYPE type;
5019 unsigned int i;
5020 HRESULT hr;
5022 /* Order matters. Specifically, BattleZone II (full version) expects the
5023 * 16-bit depth formats to be listed before the 24 and 32 ones. */
5024 static const enum wined3d_format_id formats[] =
5026 WINED3DFMT_S1_UINT_D15_UNORM,
5027 WINED3DFMT_D16_UNORM,
5028 WINED3DFMT_X8D24_UNORM,
5029 WINED3DFMT_S4X4_UINT_D24_UNORM,
5030 WINED3DFMT_D24_UNORM_S8_UINT,
5031 WINED3DFMT_D32_UNORM,
5034 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
5035 iface, debugstr_guid(device_iid), callback, context);
5037 if (!callback) return DDERR_INVALIDPARAMS;
5039 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
5040 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
5041 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
5043 TRACE("Asked for HAL device.\n");
5044 type = WINED3DDEVTYPE_HAL;
5046 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
5047 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
5049 TRACE("Asked for SW device.\n");
5050 type = WINED3DDEVTYPE_SW;
5052 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
5054 TRACE("Asked for REF device.\n");
5055 type = WINED3DDEVTYPE_REF;
5057 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
5059 TRACE("Asked for NULLREF device.\n");
5060 type = WINED3DDEVTYPE_NULLREF;
5062 else
5064 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
5065 type = WINED3DDEVTYPE_HAL;
5068 EnterCriticalSection(&ddraw_cs);
5069 /* We need an adapter format from somewhere to please wined3d and WGL.
5070 * Use the current display mode. So far all cards offer the same depth
5071 * stencil format for all modes, but if some do not and applications do
5072 * not like that we'll have to find some workaround, like iterating over
5073 * all imaginable formats and collecting all the depth stencil formats we
5074 * can get. */
5075 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &d3ddm);
5077 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
5079 hr = wined3d_check_device_format(This->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
5080 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
5081 if (SUCCEEDED(hr))
5083 DDPIXELFORMAT pformat;
5085 memset(&pformat, 0, sizeof(pformat));
5086 pformat.dwSize = sizeof(pformat);
5087 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
5089 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
5090 hr = callback(&pformat, context);
5091 if (hr != DDENUMRET_OK)
5093 TRACE("Format enumeration cancelled by application.\n");
5094 LeaveCriticalSection(&ddraw_cs);
5095 return D3D_OK;
5099 TRACE("End of enumeration.\n");
5101 LeaveCriticalSection(&ddraw_cs);
5102 return D3D_OK;
5105 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
5106 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
5108 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
5110 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
5111 iface, debugstr_guid(device_iid), callback, context);
5113 return d3d7_EnumZBufferFormats(&This->IDirect3D7_iface, device_iid, callback, context);
5116 /*****************************************************************************
5117 * IDirect3D7::EvictManagedTextures
5119 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
5120 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
5122 * Version 3 and 7
5124 * Returns:
5125 * D3D_OK, because it's a stub
5127 *****************************************************************************/
5128 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
5130 FIXME("iface %p stub!\n", iface);
5132 /* TODO: Just enumerate resources using IWineD3DDevice_EnumResources(),
5133 * then unload surfaces / textures. */
5135 return D3D_OK;
5138 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
5140 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
5142 TRACE("iface %p.\n", iface);
5144 return d3d7_EvictManagedTextures(&This->IDirect3D7_iface);
5147 /*****************************************************************************
5148 * IDirect3DImpl_GetCaps
5150 * This function retrieves the device caps from wined3d
5151 * and converts it into a D3D7 and D3D - D3D3 structure
5152 * This is a helper function called from various places in ddraw
5154 * Params:
5155 * wined3d: The interface to get the caps from
5156 * desc1: Old D3D <3 structure to fill (needed)
5157 * desc7: D3D7 device desc structure to fill (needed)
5159 * Returns
5160 * D3D_OK on success, or the return value of IWineD3D::GetCaps
5162 *****************************************************************************/
5163 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
5165 WINED3DCAPS wined3d_caps;
5166 HRESULT hr;
5168 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
5170 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
5172 EnterCriticalSection(&ddraw_cs);
5173 hr = wined3d_get_device_caps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
5174 LeaveCriticalSection(&ddraw_cs);
5175 if (FAILED(hr))
5177 WARN("Failed to get device caps, hr %#x.\n", hr);
5178 return hr;
5181 /* Copy the results into the d3d7 and d3d3 structures */
5182 desc7->dwDevCaps = wined3d_caps.DevCaps;
5183 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
5184 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
5185 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
5186 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
5187 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
5188 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
5189 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
5190 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
5191 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
5192 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
5194 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
5195 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
5197 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
5198 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
5199 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
5200 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
5202 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
5203 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
5204 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
5205 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
5207 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
5208 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
5210 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
5211 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
5213 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
5214 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
5216 /* Remove all non-d3d7 caps */
5217 desc7->dwDevCaps &= (
5218 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
5219 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
5220 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
5221 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
5222 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
5223 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
5224 D3DDEVCAPS_HWRASTERIZATION);
5226 desc7->dwStencilCaps &= (
5227 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
5228 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
5229 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
5231 /* FVF caps ?*/
5233 desc7->dwTextureOpCaps &= (
5234 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
5235 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
5236 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
5237 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
5238 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
5239 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5240 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
5241 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
5243 desc7->dwVertexProcessingCaps &= (
5244 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
5245 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
5247 desc7->dpcLineCaps.dwMiscCaps &= (
5248 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
5249 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
5250 D3DPMISCCAPS_CULLCCW);
5252 desc7->dpcLineCaps.dwRasterCaps &= (
5253 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
5254 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
5255 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
5256 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
5257 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
5258 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
5259 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
5260 D3DPRASTERCAPS_ZFOG);
5262 desc7->dpcLineCaps.dwZCmpCaps &= (
5263 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5264 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5265 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5267 desc7->dpcLineCaps.dwSrcBlendCaps &= (
5268 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5269 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5270 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5271 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5272 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5274 desc7->dpcLineCaps.dwDestBlendCaps &= (
5275 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5276 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5277 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5278 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5279 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5281 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
5282 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5283 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5284 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5286 desc7->dpcLineCaps.dwShadeCaps &= (
5287 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
5288 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
5289 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
5290 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
5291 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
5292 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
5293 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
5295 desc7->dpcLineCaps.dwTextureCaps &= (
5296 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
5297 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
5298 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
5299 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
5301 desc7->dpcLineCaps.dwTextureFilterCaps &= (
5302 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
5303 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
5304 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
5305 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
5306 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
5307 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
5309 desc7->dpcLineCaps.dwTextureBlendCaps &= (
5310 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
5311 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
5312 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
5314 desc7->dpcLineCaps.dwTextureAddressCaps &= (
5315 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
5316 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
5318 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
5320 /* DirectX7 always has the np2 flag set, no matter what the card
5321 * supports. Some old games (Rollcage) check the caps incorrectly.
5322 * If wined3d supports nonpow2 textures it also has np2 conditional
5323 * support. */
5324 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
5327 /* Fill the missing members, and do some fixup */
5328 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
5329 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
5330 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
5331 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
5332 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
5333 desc7->dpcLineCaps.dwStippleWidth = 32;
5334 desc7->dpcLineCaps.dwStippleHeight = 32;
5335 /* Use the same for the TriCaps */
5336 desc7->dpcTriCaps = desc7->dpcLineCaps;
5338 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
5339 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
5340 desc7->dwMinTextureWidth = 1;
5341 desc7->dwMinTextureHeight = 1;
5343 /* Convert DWORDs safely to WORDs */
5344 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
5345 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
5346 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
5347 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
5349 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
5350 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
5351 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
5352 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
5354 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
5356 desc7->dwReserved1 = 0;
5357 desc7->dwReserved2 = 0;
5358 desc7->dwReserved3 = 0;
5359 desc7->dwReserved4 = 0;
5361 /* Fill the old structure */
5362 memset(desc1, 0, sizeof(*desc1));
5363 desc1->dwSize = sizeof(D3DDEVICEDESC);
5364 desc1->dwFlags = D3DDD_COLORMODEL
5365 | D3DDD_DEVCAPS
5366 | D3DDD_TRANSFORMCAPS
5367 | D3DDD_BCLIPPING
5368 | D3DDD_LIGHTINGCAPS
5369 | D3DDD_LINECAPS
5370 | D3DDD_TRICAPS
5371 | D3DDD_DEVICERENDERBITDEPTH
5372 | D3DDD_DEVICEZBUFFERBITDEPTH
5373 | D3DDD_MAXBUFFERSIZE
5374 | D3DDD_MAXVERTEXCOUNT;
5376 desc1->dcmColorModel = D3DCOLOR_RGB;
5377 desc1->dwDevCaps = desc7->dwDevCaps;
5378 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
5379 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
5380 desc1->bClipping = TRUE;
5381 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
5382 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
5383 | D3DLIGHTCAPS_PARALLELPOINT
5384 | D3DLIGHTCAPS_POINT
5385 | D3DLIGHTCAPS_SPOT;
5387 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
5388 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
5390 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
5391 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
5392 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
5393 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
5394 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
5395 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
5396 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
5397 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
5398 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
5399 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
5400 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
5401 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
5402 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
5404 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
5405 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
5406 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
5407 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
5408 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
5409 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
5410 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
5411 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
5412 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
5413 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
5414 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
5415 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
5416 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
5418 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
5419 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
5420 desc1->dwMaxBufferSize = 0;
5421 desc1->dwMaxVertexCount = 65536;
5422 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
5423 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
5424 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
5425 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
5426 desc1->dwMinStippleWidth = 1;
5427 desc1->dwMinStippleHeight = 1;
5428 desc1->dwMaxStippleWidth = 32;
5429 desc1->dwMaxStippleHeight = 32;
5430 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
5431 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
5432 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
5433 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
5434 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
5435 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
5436 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
5437 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
5438 desc1->dwStencilCaps = desc7->dwStencilCaps;
5439 desc1->dwFVFCaps = desc7->dwFVFCaps;
5440 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
5441 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
5442 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
5444 return DD_OK;
5447 /*****************************************************************************
5448 * IDirectDraw7 VTable
5449 *****************************************************************************/
5450 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
5452 /* IUnknown */
5453 ddraw7_QueryInterface,
5454 ddraw7_AddRef,
5455 ddraw7_Release,
5456 /* IDirectDraw */
5457 ddraw7_Compact,
5458 ddraw7_CreateClipper,
5459 ddraw7_CreatePalette,
5460 ddraw7_CreateSurface,
5461 ddraw7_DuplicateSurface,
5462 ddraw7_EnumDisplayModes,
5463 ddraw7_EnumSurfaces,
5464 ddraw7_FlipToGDISurface,
5465 ddraw7_GetCaps,
5466 ddraw7_GetDisplayMode,
5467 ddraw7_GetFourCCCodes,
5468 ddraw7_GetGDISurface,
5469 ddraw7_GetMonitorFrequency,
5470 ddraw7_GetScanLine,
5471 ddraw7_GetVerticalBlankStatus,
5472 ddraw7_Initialize,
5473 ddraw7_RestoreDisplayMode,
5474 ddraw7_SetCooperativeLevel,
5475 ddraw7_SetDisplayMode,
5476 ddraw7_WaitForVerticalBlank,
5477 /* IDirectDraw2 */
5478 ddraw7_GetAvailableVidMem,
5479 /* IDirectDraw3 */
5480 ddraw7_GetSurfaceFromDC,
5481 /* IDirectDraw4 */
5482 ddraw7_RestoreAllSurfaces,
5483 ddraw7_TestCooperativeLevel,
5484 ddraw7_GetDeviceIdentifier,
5485 /* IDirectDraw7 */
5486 ddraw7_StartModeTest,
5487 ddraw7_EvaluateMode
5490 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5492 /* IUnknown */
5493 ddraw4_QueryInterface,
5494 ddraw4_AddRef,
5495 ddraw4_Release,
5496 /* IDirectDraw */
5497 ddraw4_Compact,
5498 ddraw4_CreateClipper,
5499 ddraw4_CreatePalette,
5500 ddraw4_CreateSurface,
5501 ddraw4_DuplicateSurface,
5502 ddraw4_EnumDisplayModes,
5503 ddraw4_EnumSurfaces,
5504 ddraw4_FlipToGDISurface,
5505 ddraw4_GetCaps,
5506 ddraw4_GetDisplayMode,
5507 ddraw4_GetFourCCCodes,
5508 ddraw4_GetGDISurface,
5509 ddraw4_GetMonitorFrequency,
5510 ddraw4_GetScanLine,
5511 ddraw4_GetVerticalBlankStatus,
5512 ddraw4_Initialize,
5513 ddraw4_RestoreDisplayMode,
5514 ddraw4_SetCooperativeLevel,
5515 ddraw4_SetDisplayMode,
5516 ddraw4_WaitForVerticalBlank,
5517 /* IDirectDraw2 */
5518 ddraw4_GetAvailableVidMem,
5519 /* IDirectDraw3 */
5520 ddraw4_GetSurfaceFromDC,
5521 /* IDirectDraw4 */
5522 ddraw4_RestoreAllSurfaces,
5523 ddraw4_TestCooperativeLevel,
5524 ddraw4_GetDeviceIdentifier,
5527 static const struct IDirectDraw3Vtbl ddraw3_vtbl =
5529 /* IUnknown */
5530 ddraw3_QueryInterface,
5531 ddraw3_AddRef,
5532 ddraw3_Release,
5533 /* IDirectDraw */
5534 ddraw3_Compact,
5535 ddraw3_CreateClipper,
5536 ddraw3_CreatePalette,
5537 ddraw3_CreateSurface,
5538 ddraw3_DuplicateSurface,
5539 ddraw3_EnumDisplayModes,
5540 ddraw3_EnumSurfaces,
5541 ddraw3_FlipToGDISurface,
5542 ddraw3_GetCaps,
5543 ddraw3_GetDisplayMode,
5544 ddraw3_GetFourCCCodes,
5545 ddraw3_GetGDISurface,
5546 ddraw3_GetMonitorFrequency,
5547 ddraw3_GetScanLine,
5548 ddraw3_GetVerticalBlankStatus,
5549 ddraw3_Initialize,
5550 ddraw3_RestoreDisplayMode,
5551 ddraw3_SetCooperativeLevel,
5552 ddraw3_SetDisplayMode,
5553 ddraw3_WaitForVerticalBlank,
5554 /* IDirectDraw2 */
5555 ddraw3_GetAvailableVidMem,
5556 /* IDirectDraw3 */
5557 ddraw3_GetSurfaceFromDC,
5560 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5562 /* IUnknown */
5563 ddraw2_QueryInterface,
5564 ddraw2_AddRef,
5565 ddraw2_Release,
5566 /* IDirectDraw */
5567 ddraw2_Compact,
5568 ddraw2_CreateClipper,
5569 ddraw2_CreatePalette,
5570 ddraw2_CreateSurface,
5571 ddraw2_DuplicateSurface,
5572 ddraw2_EnumDisplayModes,
5573 ddraw2_EnumSurfaces,
5574 ddraw2_FlipToGDISurface,
5575 ddraw2_GetCaps,
5576 ddraw2_GetDisplayMode,
5577 ddraw2_GetFourCCCodes,
5578 ddraw2_GetGDISurface,
5579 ddraw2_GetMonitorFrequency,
5580 ddraw2_GetScanLine,
5581 ddraw2_GetVerticalBlankStatus,
5582 ddraw2_Initialize,
5583 ddraw2_RestoreDisplayMode,
5584 ddraw2_SetCooperativeLevel,
5585 ddraw2_SetDisplayMode,
5586 ddraw2_WaitForVerticalBlank,
5587 /* IDirectDraw2 */
5588 ddraw2_GetAvailableVidMem,
5591 static const struct IDirectDrawVtbl ddraw1_vtbl =
5593 /* IUnknown */
5594 ddraw1_QueryInterface,
5595 ddraw1_AddRef,
5596 ddraw1_Release,
5597 /* IDirectDraw */
5598 ddraw1_Compact,
5599 ddraw1_CreateClipper,
5600 ddraw1_CreatePalette,
5601 ddraw1_CreateSurface,
5602 ddraw1_DuplicateSurface,
5603 ddraw1_EnumDisplayModes,
5604 ddraw1_EnumSurfaces,
5605 ddraw1_FlipToGDISurface,
5606 ddraw1_GetCaps,
5607 ddraw1_GetDisplayMode,
5608 ddraw1_GetFourCCCodes,
5609 ddraw1_GetGDISurface,
5610 ddraw1_GetMonitorFrequency,
5611 ddraw1_GetScanLine,
5612 ddraw1_GetVerticalBlankStatus,
5613 ddraw1_Initialize,
5614 ddraw1_RestoreDisplayMode,
5615 ddraw1_SetCooperativeLevel,
5616 ddraw1_SetDisplayMode,
5617 ddraw1_WaitForVerticalBlank,
5620 static const struct IDirect3D7Vtbl d3d7_vtbl =
5622 /* IUnknown methods */
5623 d3d7_QueryInterface,
5624 d3d7_AddRef,
5625 d3d7_Release,
5626 /* IDirect3D7 methods */
5627 d3d7_EnumDevices,
5628 d3d7_CreateDevice,
5629 d3d7_CreateVertexBuffer,
5630 d3d7_EnumZBufferFormats,
5631 d3d7_EvictManagedTextures
5634 static const struct IDirect3D3Vtbl d3d3_vtbl =
5636 /* IUnknown methods */
5637 d3d3_QueryInterface,
5638 d3d3_AddRef,
5639 d3d3_Release,
5640 /* IDirect3D3 methods */
5641 d3d3_EnumDevices,
5642 d3d3_CreateLight,
5643 d3d3_CreateMaterial,
5644 d3d3_CreateViewport,
5645 d3d3_FindDevice,
5646 d3d3_CreateDevice,
5647 d3d3_CreateVertexBuffer,
5648 d3d3_EnumZBufferFormats,
5649 d3d3_EvictManagedTextures
5652 static const struct IDirect3D2Vtbl d3d2_vtbl =
5654 /* IUnknown methods */
5655 d3d2_QueryInterface,
5656 d3d2_AddRef,
5657 d3d2_Release,
5658 /* IDirect3D2 methods */
5659 d3d2_EnumDevices,
5660 d3d2_CreateLight,
5661 d3d2_CreateMaterial,
5662 d3d2_CreateViewport,
5663 d3d2_FindDevice,
5664 d3d2_CreateDevice
5667 static const struct IDirect3DVtbl d3d1_vtbl =
5669 /* IUnknown methods */
5670 d3d1_QueryInterface,
5671 d3d1_AddRef,
5672 d3d1_Release,
5673 /* IDirect3D methods */
5674 d3d1_Initialize,
5675 d3d1_EnumDevices,
5676 d3d1_CreateLight,
5677 d3d1_CreateMaterial,
5678 d3d1_CreateViewport,
5679 d3d1_FindDevice
5682 /*****************************************************************************
5683 * ddraw_find_decl
5685 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5686 * if none was found.
5688 * This function is in ddraw.c and the DDraw object space because D3D7
5689 * vertex buffers are created using the IDirect3D interface to the ddraw
5690 * object, so they can be valid across D3D devices(theoretically. The ddraw
5691 * object also owns the wined3d device
5693 * Parameters:
5694 * This: Device
5695 * fvf: Fvf to find the decl for
5697 * Returns:
5698 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5700 *****************************************************************************/
5701 struct wined3d_vertex_declaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5703 struct wined3d_vertex_declaration *pDecl = NULL;
5704 HRESULT hr;
5705 int p, low, high; /* deliberately signed */
5706 struct FvfToDecl *convertedDecls = This->decls;
5708 TRACE("Searching for declaration for fvf %08x... ", fvf);
5710 low = 0;
5711 high = This->numConvertedDecls - 1;
5712 while(low <= high) {
5713 p = (low + high) >> 1;
5714 TRACE("%d ", p);
5715 if(convertedDecls[p].fvf == fvf) {
5716 TRACE("found %p\n", convertedDecls[p].decl);
5717 return convertedDecls[p].decl;
5718 } else if(convertedDecls[p].fvf < fvf) {
5719 low = p + 1;
5720 } else {
5721 high = p - 1;
5724 TRACE("not found. Creating and inserting at position %d.\n", low);
5726 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5727 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5728 if (hr != S_OK) return NULL;
5730 if(This->declArraySize == This->numConvertedDecls) {
5731 int grow = max(This->declArraySize / 2, 8);
5732 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5733 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5734 if (!convertedDecls)
5736 wined3d_vertex_declaration_decref(pDecl);
5737 return NULL;
5739 This->decls = convertedDecls;
5740 This->declArraySize += grow;
5743 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5744 convertedDecls[low].decl = pDecl;
5745 convertedDecls[low].fvf = fvf;
5746 This->numConvertedDecls++;
5748 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5749 return pDecl;
5752 static inline struct IDirectDrawImpl *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5754 return CONTAINING_RECORD(device_parent, struct IDirectDrawImpl, device_parent);
5757 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5758 struct wined3d_device *device)
5760 TRACE("device_parent %p, device %p.\n", device_parent, device);
5763 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
5764 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5765 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
5767 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5768 IDirectDrawSurfaceImpl *surf = NULL;
5769 UINT i = 0;
5770 DDSCAPS2 searchcaps = ddraw->tex_root->surface_desc.ddsCaps;
5772 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5773 "\tpool %#x, level %u, face %u, surface %p.\n",
5774 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5776 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5777 switch(face)
5779 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5780 TRACE("Asked for positive x\n");
5781 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5783 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5785 surf = ddraw->tex_root; break;
5786 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5787 TRACE("Asked for negative x\n");
5788 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5789 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5790 TRACE("Asked for positive y\n");
5791 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5792 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5793 TRACE("Asked for negative y\n");
5794 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5795 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5796 TRACE("Asked for positive z\n");
5797 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5798 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5799 TRACE("Asked for negative z\n");
5800 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5801 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5804 if (!surf)
5806 IDirectDrawSurface7 *attached;
5807 IDirectDrawSurface7_GetAttachedSurface(&ddraw->tex_root->IDirectDrawSurface7_iface, &searchcaps, &attached);
5808 surf = unsafe_impl_from_IDirectDrawSurface7(attached);
5809 IDirectDrawSurface7_Release(attached);
5811 if (!surf) ERR("root search surface not found\n");
5813 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5814 while (i < level)
5816 IDirectDrawSurface7 *attached;
5817 IDirectDrawSurface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &searchcaps, &attached);
5818 if(!attached) ERR("Surface not found\n");
5819 surf = impl_from_IDirectDrawSurface7(attached);
5820 IDirectDrawSurface7_Release(attached);
5821 ++i;
5824 /* Return the surface */
5825 *surface = surf->wined3d_surface;
5826 wined3d_surface_incref(*surface);
5828 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5830 return D3D_OK;
5833 static HRESULT WINAPI findRenderTarget(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *ctx)
5835 IDirectDrawSurfaceImpl *s = impl_from_IDirectDrawSurface7(surface);
5836 IDirectDrawSurfaceImpl **target = ctx;
5838 if (!s->isRenderTarget)
5840 *target = s;
5841 IDirectDrawSurface7_Release(surface);
5842 return DDENUMRET_CANCEL;
5845 /* Recurse into the surface tree */
5846 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
5848 IDirectDrawSurface7_Release(surface);
5849 if (*target) return DDENUMRET_CANCEL;
5851 return DDENUMRET_OK;
5854 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
5855 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
5856 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5857 struct wined3d_surface **surface)
5859 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5860 IDirectDrawSurfaceImpl *d3d_surface = ddraw->d3d_target;
5861 IDirectDrawSurfaceImpl *target = NULL;
5863 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5864 "\tmultisample_quality %u, lockable %u, surface %p.\n",
5865 device_parent, container_parent, width, height, format, multisample_type,
5866 multisample_quality, lockable, surface);
5868 if (d3d_surface->isRenderTarget)
5870 IDirectDrawSurface7_EnumAttachedSurfaces(&d3d_surface->IDirectDrawSurface7_iface, &target, findRenderTarget);
5872 else
5874 target = d3d_surface;
5877 if (!target)
5879 target = ddraw->d3d_target;
5880 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", ddraw);
5883 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
5885 *surface = target->wined3d_surface;
5886 wined3d_surface_incref(*surface);
5887 target->isRenderTarget = TRUE;
5889 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
5891 return D3D_OK;
5894 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
5895 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5896 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
5898 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5899 IDirectDrawSurface7 *ddraw7;
5900 IDirectDrawSurfaceImpl *ddraw_surface;
5901 DDSURFACEDESC2 ddsd;
5902 HRESULT hr;
5904 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5905 "\tmultisample_quality %u, discard %u, surface %p.\n",
5906 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
5908 *surface = NULL;
5910 /* Create a DirectDraw surface */
5911 memset(&ddsd, 0, sizeof(ddsd));
5912 ddsd.dwSize = sizeof(ddsd);
5913 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
5914 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5915 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5916 ddsd.dwHeight = height;
5917 ddsd.dwWidth = width;
5918 if (format)
5920 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
5922 else
5924 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
5927 ddraw->depthstencil = TRUE;
5928 hr = IDirectDraw7_CreateSurface(&ddraw->IDirectDraw7_iface, &ddsd, &ddraw7, NULL);
5929 ddraw->depthstencil = FALSE;
5930 if (FAILED(hr))
5932 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
5933 return hr;
5936 ddraw_surface = impl_from_IDirectDrawSurface7(ddraw7);
5937 *surface = ddraw_surface->wined3d_surface;
5938 wined3d_surface_incref(*surface);
5939 IDirectDrawSurface7_Release(&ddraw_surface->IDirectDrawSurface7_iface);
5941 return D3D_OK;
5944 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5945 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5946 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
5948 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5949 "format %#x, pool %#x, usage %#x, volume %p.\n",
5950 device_parent, container_parent, width, height, depth,
5951 format, pool, usage, volume);
5953 ERR("Not implemented!\n");
5955 return E_NOTIMPL;
5958 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5959 WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
5961 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5962 IDirectDrawSurfaceImpl *iterator;
5963 HRESULT hr;
5965 TRACE("device_parent %p, present_parameters %p, swapchain %p.\n", device_parent, present_parameters, swapchain);
5967 hr = wined3d_swapchain_create(ddraw->wined3d_device, present_parameters,
5968 ddraw->ImplType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5969 if (FAILED(hr))
5971 WARN("Failed to create swapchain, hr %#x.\n", hr);
5972 *swapchain = NULL;
5973 return hr;
5976 ddraw->d3d_target->wined3d_swapchain = *swapchain;
5977 iterator = ddraw->d3d_target->complex_array[0];
5978 while (iterator)
5980 iterator->wined3d_swapchain = *swapchain;
5981 iterator = iterator->complex_array[0];
5984 return hr;
5987 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5989 device_parent_wined3d_device_created,
5990 device_parent_create_surface,
5991 device_parent_create_rendertarget,
5992 device_parent_create_depth_stencil,
5993 device_parent_create_volume,
5994 device_parent_create_swapchain,
5997 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5999 HRESULT hr;
6000 HDC hDC;
6002 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
6003 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
6004 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
6005 ddraw->IDirectDraw3_iface.lpVtbl = &ddraw3_vtbl;
6006 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
6007 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
6008 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
6009 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
6010 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
6011 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
6012 ddraw->numIfaces = 1;
6013 ddraw->ref7 = 1;
6015 /* See comments in IDirectDrawImpl_CreateNewSurface for a description of
6016 * this field. */
6017 ddraw->ImplType = DefaultSurfaceType;
6019 /* Get the current screen settings. */
6020 hDC = GetDC(0);
6021 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
6022 ReleaseDC(0, hDC);
6023 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
6024 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
6026 ddraw->wineD3D = wined3d_create(7, &ddraw->IDirectDraw7_iface);
6027 if (!ddraw->wineD3D)
6029 WARN("Failed to create a wined3d object.\n");
6030 return E_OUTOFMEMORY;
6033 hr = wined3d_device_create(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type,
6034 NULL, 0, &ddraw->device_parent, &ddraw->wined3d_device);
6035 if (FAILED(hr))
6037 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
6038 wined3d_decref(ddraw->wineD3D);
6039 return hr;
6042 /* Get the amount of video memory */
6043 ddraw->total_vidmem = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
6045 list_init(&ddraw->surface_list);
6047 return DD_OK;