ddraw: COM cleanup for the IDirect3D2 iface.
[wine.git] / dlls / ddraw / ddraw.c
blobc398237febd96354f03bd1a45ac6627af9cf2ffa
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 void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
44 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
46 ddraw_null_wined3d_object_destroyed,
49 static inline IDirectDrawImpl *ddraw_from_ddraw1(IDirectDraw *iface)
51 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw_vtbl));
54 static inline IDirectDrawImpl *ddraw_from_ddraw2(IDirectDraw2 *iface)
56 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw2_vtbl));
59 static inline IDirectDrawImpl *ddraw_from_ddraw3(IDirectDraw3 *iface)
61 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw3_vtbl));
64 static inline IDirectDrawImpl *ddraw_from_ddraw4(IDirectDraw4 *iface)
66 return (IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(IDirectDrawImpl, IDirectDraw4_vtbl));
69 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
71 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
74 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
76 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
79 /*****************************************************************************
80 * IUnknown Methods
81 *****************************************************************************/
83 /*****************************************************************************
84 * IDirectDraw7::QueryInterface
86 * Queries different interfaces of the DirectDraw object. It can return
87 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
88 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
89 * method.
90 * The returned interface is AddRef()-ed before it's returned
92 * Used for version 1, 2, 4 and 7
94 * Params:
95 * refiid: Interface ID asked for
96 * obj: Used to return the interface pointer
98 * Returns:
99 * S_OK if an interface was found
100 * E_NOINTERFACE if the requested interface wasn't found
102 *****************************************************************************/
103 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
105 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
107 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
109 /* Can change surface impl type */
110 EnterCriticalSection(&ddraw_cs);
112 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
113 *obj = NULL;
115 if(!refiid)
117 LeaveCriticalSection(&ddraw_cs);
118 return DDERR_INVALIDPARAMS;
121 /* Check DirectDraw Interfaces */
122 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
123 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
125 *obj = This;
126 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
128 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
130 *obj = &This->IDirectDraw4_vtbl;
131 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
133 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
135 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
136 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
137 *obj = NULL;
138 LeaveCriticalSection(&ddraw_cs);
139 return E_NOINTERFACE;
141 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
143 *obj = &This->IDirectDraw2_vtbl;
144 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
146 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
148 *obj = &This->IDirectDraw_vtbl;
149 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
152 /* Direct3D
153 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
154 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
155 * who had this idea and why. The older interfaces can query and IDirect3D version
156 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
157 * and messy to implement with the common creation function, so it has been left out here.
159 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
160 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
161 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
162 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
164 /* Check the surface implementation */
165 if(This->ImplType == SURFACE_UNKNOWN)
167 /* Apps may create the IDirect3D Interface before the primary surface.
168 * set the surface implementation */
169 This->ImplType = SURFACE_OPENGL;
170 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
172 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
174 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
175 ERR(" (%p) You may want to contact wine-devel for help\n", This);
176 /* Should I assert(0) here??? */
178 else if(This->ImplType != SURFACE_OPENGL)
180 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
181 /* Do not abort here, only reject 3D Device creation */
184 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
186 This->d3dversion = 1;
187 *obj = &This->IDirect3D_iface;
188 TRACE(" returning Direct3D interface at %p.\n", *obj);
190 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
192 This->d3dversion = 2;
193 *obj = &This->IDirect3D2_iface;
194 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
196 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
198 This->d3dversion = 3;
199 *obj = &This->IDirect3D3_vtbl;
200 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
202 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
204 This->d3dversion = 7;
205 *obj = &This->IDirect3D7_vtbl;
206 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
209 else if (IsEqualGUID(refiid, &IID_IWineD3DDeviceParent))
211 *obj = &This->device_parent_vtbl;
214 /* Unknown interface */
215 else
217 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
218 LeaveCriticalSection(&ddraw_cs);
219 return E_NOINTERFACE;
222 IUnknown_AddRef( (IUnknown *) *obj );
223 LeaveCriticalSection(&ddraw_cs);
224 return S_OK;
227 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
229 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
231 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw4(iface), riid, object);
234 static HRESULT WINAPI ddraw3_QueryInterface(IDirectDraw3 *iface, REFIID riid, void **object)
236 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
238 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw3(iface), riid, object);
241 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
243 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
245 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw2(iface), riid, object);
248 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
250 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
252 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_ddraw1(iface), riid, object);
255 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
257 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
259 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_d3d7(iface), riid, object);
262 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
264 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
266 return ddraw7_QueryInterface((IDirectDraw7 *)ddraw_from_d3d3(iface), riid, object);
269 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
271 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
273 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
275 return ddraw7_QueryInterface((IDirectDraw7 *)This, riid, object);
278 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
280 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
282 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
284 return ddraw7_QueryInterface((IDirectDraw7 *)This, riid, object);
287 /*****************************************************************************
288 * IDirectDraw7::AddRef
290 * Increases the interfaces refcount, basically
292 * DDraw refcounting is a bit tricky. The different DirectDraw interface
293 * versions have individual refcounts, but the IDirect3D interfaces do not.
294 * All interfaces are from one object, that means calling QueryInterface on an
295 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
296 * IDirectDrawImpl object.
298 * That means all AddRef and Release implementations of IDirectDrawX work
299 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
300 * except of IDirect3D7 which thunks to IDirectDraw7
302 * Returns: The new refcount
304 *****************************************************************************/
305 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
307 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
308 ULONG ref = InterlockedIncrement(&This->ref7);
310 TRACE("%p increasing refcount to %u.\n", This, ref);
312 if(ref == 1) InterlockedIncrement(&This->numIfaces);
314 return ref;
317 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
319 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
320 ULONG ref = InterlockedIncrement(&ddraw->ref4);
322 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
324 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
326 return ref;
329 static ULONG WINAPI ddraw3_AddRef(IDirectDraw3 *iface)
331 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
332 ULONG ref = InterlockedIncrement(&ddraw->ref3);
334 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
336 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
338 return ref;
341 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
343 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
344 ULONG ref = InterlockedIncrement(&ddraw->ref2);
346 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
348 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
350 return ref;
353 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
355 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
356 ULONG ref = InterlockedIncrement(&ddraw->ref1);
358 TRACE("%p increasing refcount to %u.\n", ddraw, ref);
360 if (ref == 1) InterlockedIncrement(&ddraw->numIfaces);
362 return ref;
365 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
367 TRACE("iface %p.\n", iface);
369 return ddraw7_AddRef((IDirectDraw7 *)ddraw_from_d3d7(iface));
372 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
374 TRACE("iface %p.\n", iface);
376 return ddraw1_AddRef((IDirectDraw *)&ddraw_from_d3d3(iface)->IDirectDraw_vtbl);
379 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
381 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
383 TRACE("iface %p.\n", iface);
385 return ddraw1_AddRef((IDirectDraw *)&This->IDirectDraw_vtbl);
388 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
390 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
392 TRACE("iface %p.\n", iface);
394 return ddraw1_AddRef((IDirectDraw *)&This->IDirectDraw_vtbl);
397 /*****************************************************************************
398 * ddraw_destroy
400 * Destroys a ddraw object if all refcounts are 0. This is to share code
401 * between the IDirectDrawX::Release functions
403 * Params:
404 * This: DirectDraw object to destroy
406 *****************************************************************************/
407 static void ddraw_destroy(IDirectDrawImpl *This)
409 IDirectDraw7_SetCooperativeLevel((IDirectDraw7 *)This, NULL, DDSCL_NORMAL);
410 IDirectDraw7_RestoreDisplayMode((IDirectDraw7 *)This);
412 /* Destroy the device window if we created one */
413 if(This->devicewindow != 0)
415 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
416 DestroyWindow(This->devicewindow);
417 This->devicewindow = 0;
420 EnterCriticalSection(&ddraw_cs);
421 list_remove(&This->ddraw_list_entry);
422 LeaveCriticalSection(&ddraw_cs);
424 /* Release the attached WineD3D stuff */
425 IWineD3DDevice_Release(This->wineD3DDevice);
426 IWineD3D_Release(This->wineD3D);
428 /* Now free the object */
429 HeapFree(GetProcessHeap(), 0, This);
432 /*****************************************************************************
433 * IDirectDraw7::Release
435 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
437 * Returns: The new refcount
438 *****************************************************************************/
439 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
441 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
442 ULONG ref = InterlockedDecrement(&This->ref7);
444 TRACE("%p decreasing refcount to %u.\n", This, ref);
446 if (!ref && !InterlockedDecrement(&This->numIfaces))
447 ddraw_destroy(This);
449 return ref;
452 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
454 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
455 ULONG ref = InterlockedDecrement(&ddraw->ref4);
457 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
459 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
460 ddraw_destroy(ddraw);
462 return ref;
465 static ULONG WINAPI ddraw3_Release(IDirectDraw3 *iface)
467 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
468 ULONG ref = InterlockedDecrement(&ddraw->ref3);
470 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
472 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
473 ddraw_destroy(ddraw);
475 return ref;
478 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
480 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
481 ULONG ref = InterlockedDecrement(&ddraw->ref2);
483 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
485 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
486 ddraw_destroy(ddraw);
488 return ref;
491 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
493 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
494 ULONG ref = InterlockedDecrement(&ddraw->ref1);
496 TRACE("%p decreasing refcount to %u.\n", ddraw, ref);
498 if (!ref && !InterlockedDecrement(&ddraw->numIfaces))
499 ddraw_destroy(ddraw);
501 return ref;
504 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
506 TRACE("iface %p.\n", iface);
508 return ddraw7_Release((IDirectDraw7 *)ddraw_from_d3d7(iface));
511 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
513 TRACE("iface %p.\n", iface);
515 return ddraw1_Release((IDirectDraw *)&ddraw_from_d3d3(iface)->IDirectDraw_vtbl);
518 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
520 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
522 TRACE("iface %p.\n", iface);
524 return ddraw1_Release((IDirectDraw *)&This->IDirectDraw_vtbl);
527 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
529 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
531 TRACE("iface %p.\n", iface);
533 return ddraw1_Release((IDirectDraw *)&This->IDirectDraw_vtbl);
536 /*****************************************************************************
537 * IDirectDraw methods
538 *****************************************************************************/
540 /*****************************************************************************
541 * IDirectDraw7::SetCooperativeLevel
543 * Sets the cooperative level for the DirectDraw object, and the window
544 * assigned to it. The cooperative level determines the general behavior
545 * of the DirectDraw application
547 * Warning: This is quite tricky, as it's not really documented which
548 * cooperative levels can be combined with each other. If a game fails
549 * after this function, try to check the cooperative levels passed on
550 * Windows, and if it returns something different.
552 * If you think that this function caused the failure because it writes a
553 * fixme, be sure to run again with a +ddraw trace.
555 * What is known about cooperative levels (See the ddraw modes test):
556 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
557 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
558 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
559 * DDSCL_FULLSCREEN can be activated
560 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
562 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
563 * DDSCL_SETFOCUSWINDOW (partially),
564 * DDSCL_MULTITHREADED (work in progress)
566 * Unhandled flags, which should be implemented
567 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
568 * expect any difference to a normal window for wine)
569 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
570 * rendering (Possible test case: Half-Life)
572 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
574 * These don't seem very important for wine:
575 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
577 * Returns:
578 * DD_OK if the cooperative level was set successfully
579 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
580 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
581 * (Probably others too, have to investigate)
583 *****************************************************************************/
584 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
586 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
587 HWND window;
589 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
590 DDRAW_dump_cooperativelevel(cooplevel);
592 EnterCriticalSection(&ddraw_cs);
594 /* Get the old window */
595 window = This->dest_window;
597 /* Tests suggest that we need one of them: */
598 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
599 DDSCL_NORMAL |
600 DDSCL_EXCLUSIVE )))
602 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
603 LeaveCriticalSection(&ddraw_cs);
604 return DDERR_INVALIDPARAMS;
607 /* Handle those levels first which set various hwnds */
608 if(cooplevel & DDSCL_SETFOCUSWINDOW)
610 /* This isn't compatible with a lot of flags */
611 if(cooplevel & ( DDSCL_MULTITHREADED |
612 DDSCL_CREATEDEVICEWINDOW |
613 DDSCL_FPUSETUP |
614 DDSCL_FPUPRESERVE |
615 DDSCL_ALLOWREBOOT |
616 DDSCL_ALLOWMODEX |
617 DDSCL_SETDEVICEWINDOW |
618 DDSCL_NORMAL |
619 DDSCL_EXCLUSIVE |
620 DDSCL_FULLSCREEN ) )
622 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
623 LeaveCriticalSection(&ddraw_cs);
624 return DDERR_INVALIDPARAMS;
627 if( (This->cooperative_level & DDSCL_EXCLUSIVE) && window )
629 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
630 LeaveCriticalSection(&ddraw_cs);
631 return DDERR_HWNDALREADYSET;
634 This->focuswindow = hwnd;
635 /* Won't use the hwnd param for anything else */
636 hwnd = NULL;
638 /* Use the focus window for drawing too */
639 This->dest_window = This->focuswindow;
641 /* Destroy the device window, if we have one */
642 if(This->devicewindow)
644 DestroyWindow(This->devicewindow);
645 This->devicewindow = NULL;
648 LeaveCriticalSection(&ddraw_cs);
649 return DD_OK;
652 if(cooplevel & DDSCL_EXCLUSIVE)
654 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
656 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
657 LeaveCriticalSection(&ddraw_cs);
658 return DDERR_INVALIDPARAMS;
661 else if( !(cooplevel & DDSCL_NORMAL) )
663 TRACE("(%p) SetCooperativeLevel needs at least SetFocusWindow or Exclusive or Normal mode\n", This);
664 LeaveCriticalSection(&ddraw_cs);
665 return DDERR_INVALIDPARAMS;
668 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
669 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
670 IWineD3DDevice_ReleaseFocusWindow(This->wineD3DDevice);
672 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
674 if (This->cooperative_level & DDSCL_FULLSCREEN)
676 IWineD3DDevice_RestoreFullscreenWindow(This->wineD3DDevice, window);
678 if (cooplevel & DDSCL_FULLSCREEN)
680 WINED3DDISPLAYMODE display_mode;
682 IWineD3D_GetAdapterDisplayMode(This->wineD3D, WINED3DADAPTER_DEFAULT, &display_mode);
683 IWineD3DDevice_SetupFullscreenWindow(This->wineD3DDevice, hwnd, display_mode.Width, display_mode.Height);
687 if ((cooplevel & DDSCL_EXCLUSIVE)
688 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
690 HRESULT hr = IWineD3DDevice_AcquireFocusWindow(This->wineD3DDevice, hwnd);
691 if (FAILED(hr))
693 ERR("Failed to acquire focus window, hr %#x.\n", hr);
694 LeaveCriticalSection(&ddraw_cs);
695 return hr;
699 /* Don't override focus windows or private device windows */
700 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
701 This->dest_window = hwnd;
703 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
705 /* Don't create a device window if a focus window is set */
706 if( !(This->focuswindow) )
708 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
709 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
710 NULL, NULL, NULL, NULL);
711 if (!devicewindow)
713 ERR("Failed to create window, last error %#x.\n", GetLastError());
714 LeaveCriticalSection(&ddraw_cs);
715 return E_FAIL;
718 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
719 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
721 This->devicewindow = devicewindow;
722 This->dest_window = devicewindow;
726 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
728 /* Enable thread safety in wined3d */
729 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
732 /* Unhandled flags */
733 if(cooplevel & DDSCL_ALLOWREBOOT)
734 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
735 if(cooplevel & DDSCL_ALLOWMODEX)
736 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
737 if(cooplevel & DDSCL_FPUSETUP)
738 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
740 /* Store the cooperative_level */
741 This->cooperative_level = cooplevel;
742 TRACE("SetCooperativeLevel retuning DD_OK\n");
743 LeaveCriticalSection(&ddraw_cs);
744 return DD_OK;
747 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
749 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
751 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw4(iface), window, flags);
754 static HRESULT WINAPI ddraw3_SetCooperativeLevel(IDirectDraw3 *iface, HWND window, DWORD flags)
756 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
758 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw3(iface), window, flags);
761 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
763 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
765 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw2(iface), window, flags);
768 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
770 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
772 return ddraw7_SetCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw1(iface), window, flags);
775 /*****************************************************************************
777 * Helper function for SetDisplayMode and RestoreDisplayMode
779 * Implements DirectDraw's SetDisplayMode, but ignores the value of
780 * ForceRefreshRate, since it is already handled by
781 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
782 * without worrying that ForceRefreshRate will override the refresh rate. For
783 * argument and return value documentation, see
784 * ddraw7_SetDisplayMode.
786 *****************************************************************************/
787 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
788 DWORD BPP, DWORD RefreshRate, DWORD Flags)
790 WINED3DDISPLAYMODE Mode;
791 HRESULT hr;
793 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
794 Height, BPP, RefreshRate, Flags);
796 EnterCriticalSection(&ddraw_cs);
797 if( !Width || !Height )
799 ERR("Width %u, Height %u, what to do?\n", Width, Height);
800 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
801 LeaveCriticalSection(&ddraw_cs);
802 return DD_OK;
805 /* Check the exclusive mode
806 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
807 return DDERR_NOEXCLUSIVEMODE;
808 * This is WRONG. Don't know if the SDK is completely
809 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
810 * is returned, but Half-Life 1.1.1.1 (Steam version)
811 * depends on this
814 Mode.Width = Width;
815 Mode.Height = Height;
816 Mode.RefreshRate = RefreshRate;
817 switch(BPP)
819 case 8: Mode.Format = WINED3DFMT_P8_UINT; break;
820 case 15: Mode.Format = WINED3DFMT_B5G5R5X1_UNORM; break;
821 case 16: Mode.Format = WINED3DFMT_B5G6R5_UNORM; break;
822 case 24: Mode.Format = WINED3DFMT_B8G8R8_UNORM; break;
823 case 32: Mode.Format = WINED3DFMT_B8G8R8X8_UNORM; break;
826 /* TODO: The possible return values from msdn suggest that
827 * the screen mode can't be changed if a surface is locked
828 * or some drawing is in progress
831 /* TODO: Lose the primary surface */
832 hr = IWineD3DDevice_SetDisplayMode(ddraw->wineD3DDevice, 0, &Mode);
833 IWineD3DDevice_RestoreFullscreenWindow(ddraw->wineD3DDevice, ddraw->dest_window);
834 IWineD3DDevice_SetupFullscreenWindow(ddraw->wineD3DDevice, ddraw->dest_window, Width, Height);
835 LeaveCriticalSection(&ddraw_cs);
836 switch(hr)
838 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
839 default: return hr;
843 /*****************************************************************************
844 * IDirectDraw7::SetDisplayMode
846 * Sets the display screen resolution, color depth and refresh frequency
847 * when in fullscreen mode (in theory).
848 * Possible return values listed in the SDK suggest that this method fails
849 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
850 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
851 * It seems to be valid to pass 0 for With and Height, this has to be tested
852 * It could mean that the current video mode should be left as-is. (But why
853 * call it then?)
855 * Params:
856 * Height, Width: Screen dimension
857 * BPP: Color depth in Bits per pixel
858 * Refreshrate: Screen refresh rate
859 * Flags: Other stuff
861 * Returns
862 * DD_OK on success
864 *****************************************************************************/
865 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
866 DWORD BPP, DWORD RefreshRate, DWORD Flags)
868 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
870 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
871 iface, Width, Height, BPP, RefreshRate, Flags);
873 if (force_refresh_rate != 0)
875 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
876 RefreshRate, force_refresh_rate);
877 RefreshRate = force_refresh_rate;
880 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
883 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface,
884 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
886 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
887 iface, width, height, bpp, refresh_rate, flags);
889 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw4(iface),
890 width, height, bpp, refresh_rate, flags);
893 static HRESULT WINAPI ddraw3_SetDisplayMode(IDirectDraw3 *iface,
894 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
896 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
897 iface, width, height, bpp, refresh_rate, flags);
899 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw3(iface),
900 width, height, bpp, refresh_rate, flags);
903 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
904 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
906 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
907 iface, width, height, bpp, refresh_rate, flags);
909 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw2(iface),
910 width, height, bpp, refresh_rate, flags);
913 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
915 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
917 return ddraw7_SetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw1(iface), width, height, bpp, 0, 0);
920 /*****************************************************************************
921 * IDirectDraw7::RestoreDisplayMode
923 * Restores the display mode to what it was at creation time. Basically.
925 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
926 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
927 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
928 * -> DD_1 is released. The screen should be left at 1024x768x32.
929 * -> DD_2 is released. The screen should be set to 1400x1050x32
930 * This case is unhandled right now, but Empire Earth does it this way.
931 * (But perhaps there is something in SetCooperativeLevel to prevent this)
933 * The msdn says that this method resets the display mode to what it was before
934 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
936 * Returns
937 * DD_OK on success
938 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
940 *****************************************************************************/
941 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
943 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
945 TRACE("iface %p.\n", iface);
947 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
950 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
952 TRACE("iface %p.\n", iface);
954 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw4(iface));
957 static HRESULT WINAPI ddraw3_RestoreDisplayMode(IDirectDraw3 *iface)
959 TRACE("iface %p.\n", iface);
961 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw3(iface));
964 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
966 TRACE("iface %p.\n", iface);
968 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw2(iface));
971 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
973 TRACE("iface %p.\n", iface);
975 return ddraw7_RestoreDisplayMode((IDirectDraw7 *)ddraw_from_ddraw1(iface));
978 /*****************************************************************************
979 * IDirectDraw7::GetCaps
981 * Returns the drives capabilities
983 * Used for version 1, 2, 4 and 7
985 * Params:
986 * DriverCaps: Structure to write the Hardware accelerated caps to
987 * HelCaps: Structure to write the emulation caps to
989 * Returns
990 * This implementation returns DD_OK only
992 *****************************************************************************/
993 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
995 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
996 DDCAPS caps;
997 WINED3DCAPS winecaps;
998 HRESULT hr;
999 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1001 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1003 /* One structure must be != NULL */
1004 if( (!DriverCaps) && (!HELCaps) )
1006 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1007 return DDERR_INVALIDPARAMS;
1010 memset(&caps, 0, sizeof(caps));
1011 memset(&winecaps, 0, sizeof(winecaps));
1012 caps.dwSize = sizeof(caps);
1013 EnterCriticalSection(&ddraw_cs);
1014 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
1015 if(FAILED(hr)) {
1016 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1017 LeaveCriticalSection(&ddraw_cs);
1018 return hr;
1021 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1022 LeaveCriticalSection(&ddraw_cs);
1023 if(FAILED(hr)) {
1024 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1025 return hr;
1028 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1029 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1030 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1031 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1032 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1033 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1034 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1035 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1036 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1037 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1038 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1039 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1040 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1041 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1042 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1044 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1045 * not to use it
1047 if(DefaultSurfaceType == SURFACE_GDI) {
1048 caps.dwCaps &= ~DDCAPS_3D;
1049 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1051 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1052 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1053 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1056 if(DriverCaps)
1058 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1059 if (TRACE_ON(ddraw))
1061 TRACE("Driver Caps :\n");
1062 DDRAW_dump_DDCAPS(DriverCaps);
1066 if(HELCaps)
1068 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1069 if (TRACE_ON(ddraw))
1071 TRACE("HEL Caps :\n");
1072 DDRAW_dump_DDCAPS(HELCaps);
1076 return DD_OK;
1079 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1081 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1083 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw4(iface), driver_caps, hel_caps);
1086 static HRESULT WINAPI ddraw3_GetCaps(IDirectDraw3 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1088 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1090 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw3(iface), driver_caps, hel_caps);
1093 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1095 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1097 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw2(iface), driver_caps, hel_caps);
1100 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1102 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1104 return ddraw7_GetCaps((IDirectDraw7 *)ddraw_from_ddraw1(iface), driver_caps, hel_caps);
1107 /*****************************************************************************
1108 * IDirectDraw7::Compact
1110 * No idea what it does, MSDN says it's not implemented.
1112 * Returns
1113 * DD_OK, but this is unchecked
1115 *****************************************************************************/
1116 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1118 TRACE("iface %p.\n", iface);
1120 return DD_OK;
1123 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1125 TRACE("iface %p.\n", iface);
1127 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw4(iface));
1130 static HRESULT WINAPI ddraw3_Compact(IDirectDraw3 *iface)
1132 TRACE("iface %p.\n", iface);
1134 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw3(iface));
1137 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1139 TRACE("iface %p.\n", iface);
1141 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw2(iface));
1144 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1146 TRACE("iface %p.\n", iface);
1148 return ddraw7_Compact((IDirectDraw7 *)ddraw_from_ddraw1(iface));
1151 /*****************************************************************************
1152 * IDirectDraw7::GetDisplayMode
1154 * Returns information about the current display mode
1156 * Exists in Version 1, 2, 4 and 7
1158 * Params:
1159 * DDSD: Address of a surface description structure to write the info to
1161 * Returns
1162 * DD_OK
1164 *****************************************************************************/
1165 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1167 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1168 HRESULT hr;
1169 WINED3DDISPLAYMODE Mode;
1170 DWORD Size;
1172 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1174 EnterCriticalSection(&ddraw_cs);
1175 /* This seems sane */
1176 if (!DDSD)
1178 LeaveCriticalSection(&ddraw_cs);
1179 return DDERR_INVALIDPARAMS;
1182 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1183 * so one method can be used for all versions (Hopefully)
1185 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1186 0 /* swapchain 0 */,
1187 &Mode);
1188 if( hr != D3D_OK )
1190 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1191 LeaveCriticalSection(&ddraw_cs);
1192 return hr;
1195 Size = DDSD->dwSize;
1196 memset(DDSD, 0, Size);
1198 DDSD->dwSize = Size;
1199 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1200 DDSD->dwWidth = Mode.Width;
1201 DDSD->dwHeight = Mode.Height;
1202 DDSD->u2.dwRefreshRate = 60;
1203 DDSD->ddsCaps.dwCaps = 0;
1204 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1205 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
1206 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1208 if(TRACE_ON(ddraw))
1210 TRACE("Returning surface desc :\n");
1211 DDRAW_dump_surface_desc(DDSD);
1214 LeaveCriticalSection(&ddraw_cs);
1215 return DD_OK;
1218 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1220 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1222 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw4(iface), surface_desc);
1225 static HRESULT WINAPI ddraw3_GetDisplayMode(IDirectDraw3 *iface, DDSURFACEDESC *surface_desc)
1227 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1229 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw3(iface), (DDSURFACEDESC2 *)surface_desc);
1232 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1234 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1236 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw2(iface), (DDSURFACEDESC2 *)surface_desc);
1239 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1241 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1243 return ddraw7_GetDisplayMode((IDirectDraw7 *)ddraw_from_ddraw1(iface), (DDSURFACEDESC2 *)surface_desc);
1246 /*****************************************************************************
1247 * IDirectDraw7::GetFourCCCodes
1249 * Returns an array of supported FourCC codes.
1251 * Exists in Version 1, 2, 4 and 7
1253 * Params:
1254 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1255 * of enumerated codes
1256 * Codes: Pointer to an array of DWORDs where the supported codes are written
1257 * to
1259 * Returns
1260 * Always returns DD_OK, as it's a stub for now
1262 *****************************************************************************/
1263 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1265 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1266 static const enum wined3d_format_id formats[] =
1268 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1269 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1270 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1272 DWORD count = 0, i, outsize;
1273 HRESULT hr;
1274 WINED3DDISPLAYMODE d3ddm;
1275 WINED3DSURFTYPE type = This->ImplType;
1277 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1279 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1280 0 /* swapchain 0 */,
1281 &d3ddm);
1283 outsize = NumCodes && Codes ? *NumCodes : 0;
1285 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
1287 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
1288 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
1289 WINED3DADAPTER_DEFAULT,
1290 WINED3DDEVTYPE_HAL,
1291 d3ddm.Format /* AdapterFormat */,
1292 0 /* usage */,
1293 WINED3DRTYPE_SURFACE,
1294 formats[i],
1295 type);
1296 if(SUCCEEDED(hr)) {
1297 if(count < outsize) {
1298 Codes[count] = formats[i];
1300 count++;
1303 if(NumCodes) {
1304 TRACE("Returning %u FourCC codes\n", count);
1305 *NumCodes = count;
1308 return DD_OK;
1311 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1313 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1315 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw4(iface), codes_count, codes);
1318 static HRESULT WINAPI ddraw3_GetFourCCCodes(IDirectDraw3 *iface, DWORD *codes_count, DWORD *codes)
1320 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1322 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw3(iface), codes_count, codes);
1325 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1327 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1329 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw2(iface), codes_count, codes);
1332 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1334 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1336 return ddraw7_GetFourCCCodes((IDirectDraw7 *)ddraw_from_ddraw1(iface), codes_count, codes);
1339 /*****************************************************************************
1340 * IDirectDraw7::GetMonitorFrequency
1342 * Returns the monitor's frequency
1344 * Exists in Version 1, 2, 4 and 7
1346 * Params:
1347 * Freq: Pointer to a DWORD to write the frequency to
1349 * Returns
1350 * Always returns DD_OK
1352 *****************************************************************************/
1353 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1355 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1357 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1358 * but for now this should make the games happy
1360 *Freq = 60;
1361 return DD_OK;
1364 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1366 TRACE("iface %p, frequency %p.\n", iface, frequency);
1368 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw4(iface), frequency);
1371 static HRESULT WINAPI ddraw3_GetMonitorFrequency(IDirectDraw3 *iface, DWORD *frequency)
1373 TRACE("iface %p, frequency %p.\n", iface, frequency);
1375 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw3(iface), frequency);
1378 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1380 TRACE("iface %p, frequency %p.\n", iface, frequency);
1382 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw2(iface), frequency);
1385 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1387 TRACE("iface %p, frequency %p.\n", iface, frequency);
1389 return ddraw7_GetMonitorFrequency((IDirectDraw7 *)ddraw_from_ddraw1(iface), frequency);
1392 /*****************************************************************************
1393 * IDirectDraw7::GetVerticalBlankStatus
1395 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1396 * too basically, but as it's a semi stub, I didn't create a function there
1398 * Params:
1399 * status: Pointer to a BOOL to be filled with the vertical blank status
1401 * Returns
1402 * DD_OK on success
1403 * DDERR_INVALIDPARAMS if status is NULL
1405 *****************************************************************************/
1406 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1408 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1410 TRACE("iface %p, status %p.\n", iface, status);
1412 /* This looks sane, the MSDN suggests it too */
1413 EnterCriticalSection(&ddraw_cs);
1414 if(!status)
1416 LeaveCriticalSection(&ddraw_cs);
1417 return DDERR_INVALIDPARAMS;
1420 *status = This->fake_vblank;
1421 This->fake_vblank = !This->fake_vblank;
1422 LeaveCriticalSection(&ddraw_cs);
1423 return DD_OK;
1426 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1428 TRACE("iface %p, status %p.\n", iface, status);
1430 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw4(iface), status);
1433 static HRESULT WINAPI ddraw3_GetVerticalBlankStatus(IDirectDraw3 *iface, BOOL *status)
1435 TRACE("iface %p, status %p.\n", iface, status);
1437 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw3(iface), status);
1440 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1442 TRACE("iface %p, status %p.\n", iface, status);
1444 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw2(iface), status);
1447 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1449 TRACE("iface %p, status %p.\n", iface, status);
1451 return ddraw7_GetVerticalBlankStatus((IDirectDraw7 *)ddraw_from_ddraw1(iface), status);
1454 /*****************************************************************************
1455 * IDirectDraw7::GetAvailableVidMem
1457 * Returns the total and free video memory
1459 * Params:
1460 * Caps: Specifies the memory type asked for
1461 * total: Pointer to a DWORD to be filled with the total memory
1462 * free: Pointer to a DWORD to be filled with the free memory
1464 * Returns
1465 * DD_OK on success
1466 * DDERR_INVALIDPARAMS of free and total are NULL
1468 *****************************************************************************/
1469 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1471 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1473 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1475 if(TRACE_ON(ddraw))
1477 TRACE("(%p) Asked for memory with description: ", This);
1478 DDRAW_dump_DDSCAPS2(Caps);
1480 EnterCriticalSection(&ddraw_cs);
1482 /* Todo: System memory vs local video memory vs non-local video memory
1483 * The MSDN also mentions differences between texture memory and other
1484 * resources, but that's not important
1487 if( (!total) && (!free) )
1489 LeaveCriticalSection(&ddraw_cs);
1490 return DDERR_INVALIDPARAMS;
1493 if(total) *total = This->total_vidmem;
1494 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1496 LeaveCriticalSection(&ddraw_cs);
1497 return DD_OK;
1500 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1501 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1503 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1505 return ddraw7_GetAvailableVidMem((IDirectDraw7 *)ddraw_from_ddraw4(iface), caps, total, free);
1508 static HRESULT WINAPI ddraw3_GetAvailableVidMem(IDirectDraw3 *iface,
1509 DDSCAPS *caps, DWORD *total, DWORD *free)
1511 DDSCAPS2 caps2;
1513 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1515 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1516 return ddraw7_GetAvailableVidMem((IDirectDraw7 *)ddraw_from_ddraw3(iface), &caps2, total, free);
1519 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1520 DDSCAPS *caps, DWORD *total, DWORD *free)
1522 DDSCAPS2 caps2;
1524 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1526 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1527 return ddraw7_GetAvailableVidMem((IDirectDraw7 *)ddraw_from_ddraw2(iface), &caps2, total, free);
1530 /*****************************************************************************
1531 * IDirectDraw7::Initialize
1533 * Initializes a DirectDraw interface.
1535 * Params:
1536 * GUID: Interface identifier. Well, don't know what this is really good
1537 * for
1539 * Returns
1540 * Returns DD_OK on the first call,
1541 * DDERR_ALREADYINITIALIZED on repeated calls
1543 *****************************************************************************/
1544 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *Guid)
1546 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1548 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(Guid));
1550 if(This->initialized)
1552 return DDERR_ALREADYINITIALIZED;
1554 else
1556 return DD_OK;
1560 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1562 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1564 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw4(iface), guid);
1567 static HRESULT WINAPI ddraw3_Initialize(IDirectDraw3 *iface, GUID *guid)
1569 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1571 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw3(iface), guid);
1574 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1576 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1578 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw2(iface), guid);
1581 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1583 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1585 return ddraw7_Initialize((IDirectDraw7 *)ddraw_from_ddraw1(iface), guid);
1588 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1590 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1592 return D3D_OK;
1595 /*****************************************************************************
1596 * IDirectDraw7::FlipToGDISurface
1598 * "Makes the surface that the GDI writes to the primary surface"
1599 * Looks like some windows specific thing we don't have to care about.
1600 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1601 * show error boxes ;)
1602 * Well, just return DD_OK.
1604 * Returns:
1605 * Always returns DD_OK
1607 *****************************************************************************/
1608 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1610 FIXME("iface %p stub!\n", iface);
1612 return DD_OK;
1615 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1617 TRACE("iface %p.\n", iface);
1619 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw4(iface));
1622 static HRESULT WINAPI ddraw3_FlipToGDISurface(IDirectDraw3 *iface)
1624 TRACE("iface %p.\n", iface);
1626 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw3(iface));
1629 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1631 TRACE("iface %p.\n", iface);
1633 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw2(iface));
1636 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1638 TRACE("iface %p.\n", iface);
1640 return ddraw7_FlipToGDISurface((IDirectDraw7 *)ddraw_from_ddraw1(iface));
1643 /*****************************************************************************
1644 * IDirectDraw7::WaitForVerticalBlank
1646 * This method allows applications to get in sync with the vertical blank
1647 * interval.
1648 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1649 * redraw the screen, most likely because of this stub
1651 * Parameters:
1652 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1653 * or DDWAITVB_BLOCKEND
1654 * h: Not used, according to MSDN
1656 * Returns:
1657 * Always returns DD_OK
1659 *****************************************************************************/
1660 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1662 static BOOL hide;
1664 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1666 /* This function is called often, so print the fixme only once */
1667 if(!hide)
1669 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1670 hide = TRUE;
1673 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1674 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1675 return DDERR_UNSUPPORTED; /* unchecked */
1677 return DD_OK;
1680 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1682 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1684 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw4(iface), flags, event);
1687 static HRESULT WINAPI ddraw3_WaitForVerticalBlank(IDirectDraw3 *iface, DWORD flags, HANDLE event)
1689 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1691 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags, event);
1694 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1696 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1698 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags, event);
1701 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1703 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1705 return ddraw7_WaitForVerticalBlank((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags, event);
1708 /*****************************************************************************
1709 * IDirectDraw7::GetScanLine
1711 * Returns the scan line that is being drawn on the monitor
1713 * Parameters:
1714 * Scanline: Address to write the scan line value to
1716 * Returns:
1717 * Always returns DD_OK
1719 *****************************************************************************/
1720 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1722 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1723 static BOOL hide = FALSE;
1724 WINED3DDISPLAYMODE Mode;
1726 TRACE("iface %p, line %p.\n", iface, Scanline);
1728 /* This function is called often, so print the fixme only once */
1729 EnterCriticalSection(&ddraw_cs);
1730 if(!hide)
1732 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1733 hide = TRUE;
1736 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1738 &Mode);
1740 /* Fake the line sweeping of the monitor */
1741 /* FIXME: We should synchronize with a source to keep the refresh rate */
1742 *Scanline = This->cur_scanline++;
1743 /* Assume 20 scan lines in the vertical blank */
1744 if (This->cur_scanline >= Mode.Height + 20)
1745 This->cur_scanline = 0;
1747 LeaveCriticalSection(&ddraw_cs);
1748 return DD_OK;
1751 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1753 TRACE("iface %p, line %p.\n", iface, line);
1755 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw4(iface), line);
1758 static HRESULT WINAPI ddraw3_GetScanLine(IDirectDraw3 *iface, DWORD *line)
1760 TRACE("iface %p, line %p.\n", iface, line);
1762 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw3(iface), line);
1765 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1767 TRACE("iface %p, line %p.\n", iface, line);
1769 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw2(iface), line);
1772 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1774 TRACE("iface %p, line %p.\n", iface, line);
1776 return ddraw7_GetScanLine((IDirectDraw7 *)ddraw_from_ddraw1(iface), line);
1779 /*****************************************************************************
1780 * IDirectDraw7::TestCooperativeLevel
1782 * Informs the application about the state of the video adapter, depending
1783 * on the cooperative level
1785 * Returns:
1786 * DD_OK if the device is in a sane state
1787 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1788 * if the state is not correct(See below)
1790 *****************************************************************************/
1791 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1793 TRACE("iface %p.\n", iface);
1795 return DD_OK;
1798 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1800 TRACE("iface %p.\n", iface);
1802 return ddraw7_TestCooperativeLevel((IDirectDraw7 *)ddraw_from_ddraw4(iface));
1805 /*****************************************************************************
1806 * IDirectDraw7::GetGDISurface
1808 * Returns the surface that GDI is treating as the primary surface.
1809 * For Wine this is the front buffer
1811 * Params:
1812 * GDISurface: Address to write the surface pointer to
1814 * Returns:
1815 * DD_OK if the surface was found
1816 * DDERR_NOTFOUND if the GDI surface wasn't found
1818 *****************************************************************************/
1819 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1821 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1822 IWineD3DSurface *Surf;
1823 IDirectDrawSurface7 *ddsurf;
1824 HRESULT hr;
1825 DDSCAPS2 ddsCaps;
1827 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1829 /* Get the back buffer from the wineD3DDevice and search its
1830 * attached surfaces for the front buffer
1832 EnterCriticalSection(&ddraw_cs);
1833 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1834 0, /* SwapChain */
1835 0, /* first back buffer*/
1836 WINED3DBACKBUFFER_TYPE_MONO,
1837 &Surf);
1839 if( (hr != D3D_OK) ||
1840 (!Surf) )
1842 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1843 LeaveCriticalSection(&ddraw_cs);
1844 return DDERR_NOTFOUND;
1847 ddsurf = IWineD3DSurface_GetParent(Surf);
1848 IWineD3DSurface_Release(Surf);
1850 /* Find the front buffer */
1851 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1852 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1853 &ddsCaps,
1854 GDISurface);
1855 if(hr != DD_OK)
1857 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1860 /* The AddRef is OK this time */
1861 LeaveCriticalSection(&ddraw_cs);
1862 return hr;
1865 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
1867 TRACE("iface %p, surface %p.\n", iface, surface);
1869 return ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw4(iface), (IDirectDrawSurface7 **)surface);
1872 static HRESULT WINAPI ddraw3_GetGDISurface(IDirectDraw3 *iface, IDirectDrawSurface **surface)
1874 IDirectDrawSurface7 *surface7;
1875 HRESULT hr;
1877 TRACE("iface %p, surface %p.\n", iface, surface);
1879 hr = ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw3(iface), &surface7);
1880 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
1882 return hr;
1885 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
1887 IDirectDrawSurface7 *surface7;
1888 HRESULT hr;
1890 TRACE("iface %p, surface %p.\n", iface, surface);
1892 hr = ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw2(iface), &surface7);
1893 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
1895 return hr;
1898 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
1900 IDirectDrawSurface7 *surface7;
1901 HRESULT hr;
1903 TRACE("iface %p, surface %p.\n", iface, surface);
1905 hr = ddraw7_GetGDISurface((IDirectDraw7 *)ddraw_from_ddraw1(iface), &surface7);
1906 *surface = surface7 ? (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
1908 return hr;
1911 struct displaymodescallback_context
1913 LPDDENUMMODESCALLBACK func;
1914 void *context;
1917 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
1919 struct displaymodescallback_context *cbcontext = context;
1920 DDSURFACEDESC desc;
1922 memcpy(&desc, surface_desc, sizeof(desc));
1923 desc.dwSize = sizeof(desc);
1925 return cbcontext->func(&desc, cbcontext->context);
1928 /*****************************************************************************
1929 * IDirectDraw7::EnumDisplayModes
1931 * Enumerates the supported Display modes. The modes can be filtered with
1932 * the DDSD parameter.
1934 * Params:
1935 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1936 * DDSD: Surface description to filter the modes
1937 * Context: Pointer passed back to the callback function
1938 * cb: Application-provided callback function
1940 * Returns:
1941 * DD_OK on success
1942 * DDERR_INVALIDPARAMS if the callback wasn't set
1944 *****************************************************************************/
1945 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
1946 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
1948 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
1949 unsigned int modenum, fmt;
1950 enum wined3d_format_id pixelformat = WINED3DFMT_UNKNOWN;
1951 WINED3DDISPLAYMODE mode;
1952 DDSURFACEDESC2 callback_sd;
1953 WINED3DDISPLAYMODE *enum_modes = NULL;
1954 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1956 static const enum wined3d_format_id checkFormatList[] =
1958 WINED3DFMT_B8G8R8X8_UNORM,
1959 WINED3DFMT_B5G6R5_UNORM,
1960 WINED3DFMT_P8_UINT,
1963 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
1964 iface, Flags, DDSD, Context, cb);
1966 EnterCriticalSection(&ddraw_cs);
1967 /* This looks sane */
1968 if(!cb)
1970 LeaveCriticalSection(&ddraw_cs);
1971 return DDERR_INVALIDPARAMS;
1974 if(DDSD)
1976 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1977 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1980 if(!(Flags & DDEDM_REFRESHRATES))
1982 enum_mode_array_size = 16;
1983 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1984 if (!enum_modes)
1986 ERR("Out of memory\n");
1987 LeaveCriticalSection(&ddraw_cs);
1988 return DDERR_OUTOFMEMORY;
1992 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1994 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1996 continue;
1999 modenum = 0;
2000 while(IWineD3D_EnumAdapterModes(This->wineD3D,
2001 WINED3DADAPTER_DEFAULT,
2002 checkFormatList[fmt],
2003 modenum++,
2004 &mode) == WINED3D_OK)
2006 if(DDSD)
2008 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
2009 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
2012 if(!(Flags & DDEDM_REFRESHRATES))
2014 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2015 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2016 * to be reduced to a single unique result in such case.
2018 BOOL found = FALSE;
2019 unsigned i;
2021 for (i = 0; i < enum_mode_count; i++)
2023 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
2024 enum_modes[i].Format == mode.Format)
2026 found = TRUE;
2027 break;
2031 if(found) continue;
2034 memset(&callback_sd, 0, sizeof(callback_sd));
2035 callback_sd.dwSize = sizeof(callback_sd);
2036 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2038 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
2039 if(Flags & DDEDM_REFRESHRATES)
2041 callback_sd.dwFlags |= DDSD_REFRESHRATE;
2042 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
2045 callback_sd.dwWidth = mode.Width;
2046 callback_sd.dwHeight = mode.Height;
2048 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
2050 /* Calc pitch and DWORD align like MSDN says */
2051 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
2052 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2054 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2055 callback_sd.u2.dwRefreshRate);
2057 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2059 TRACE("Application asked to terminate the enumeration\n");
2060 HeapFree(GetProcessHeap(), 0, enum_modes);
2061 LeaveCriticalSection(&ddraw_cs);
2062 return DD_OK;
2065 if(!(Flags & DDEDM_REFRESHRATES))
2067 if (enum_mode_count == enum_mode_array_size)
2069 WINED3DDISPLAYMODE *new_enum_modes;
2071 enum_mode_array_size *= 2;
2072 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
2074 if (!new_enum_modes)
2076 ERR("Out of memory\n");
2077 HeapFree(GetProcessHeap(), 0, enum_modes);
2078 LeaveCriticalSection(&ddraw_cs);
2079 return DDERR_OUTOFMEMORY;
2082 enum_modes = new_enum_modes;
2085 enum_modes[enum_mode_count++] = mode;
2090 TRACE("End of enumeration\n");
2091 HeapFree(GetProcessHeap(), 0, enum_modes);
2092 LeaveCriticalSection(&ddraw_cs);
2093 return DD_OK;
2096 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2097 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2099 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2100 iface, flags, surface_desc, context, callback);
2102 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw4(iface), flags,
2103 surface_desc, context, callback);
2106 static HRESULT WINAPI ddraw3_EnumDisplayModes(IDirectDraw3 *iface, DWORD flags,
2107 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2109 struct displaymodescallback_context cbcontext;
2111 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2112 iface, flags, surface_desc, context, callback);
2114 cbcontext.func = callback;
2115 cbcontext.context = context;
2117 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags,
2118 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumDisplayModesCallbackThunk);
2121 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2122 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2124 struct displaymodescallback_context cbcontext;
2126 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2127 iface, flags, surface_desc, context, callback);
2129 cbcontext.func = callback;
2130 cbcontext.context = context;
2132 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags,
2133 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumDisplayModesCallbackThunk);
2136 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2137 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2139 struct displaymodescallback_context cbcontext;
2141 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2142 iface, flags, surface_desc, context, callback);
2144 cbcontext.func = callback;
2145 cbcontext.context = context;
2147 return ddraw7_EnumDisplayModes((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags,
2148 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumDisplayModesCallbackThunk);
2151 /*****************************************************************************
2152 * IDirectDraw7::EvaluateMode
2154 * Used with IDirectDraw7::StartModeTest to test video modes.
2155 * EvaluateMode is used to pass or fail a mode, and continue with the next
2156 * mode
2158 * Params:
2159 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2160 * Timeout: Returns the amount of seconds left before the mode would have
2161 * been failed automatically
2163 * Returns:
2164 * This implementation always DD_OK, because it's a stub
2166 *****************************************************************************/
2167 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2169 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2171 /* When implementing this, implement it in WineD3D */
2173 return DD_OK;
2176 /*****************************************************************************
2177 * IDirectDraw7::GetDeviceIdentifier
2179 * Returns the device identifier, which gives information about the driver
2180 * Our device identifier is defined at the beginning of this file.
2182 * Params:
2183 * DDDI: Address for the returned structure
2184 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2186 * Returns:
2187 * On success it returns DD_OK
2188 * DDERR_INVALIDPARAMS if DDDI is NULL
2190 *****************************************************************************/
2191 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2192 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2194 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2196 if(!DDDI)
2197 return DDERR_INVALIDPARAMS;
2199 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2200 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2201 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2202 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2203 * bytes too long. So only copy the relevant part of the structure
2206 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2207 return DD_OK;
2210 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2211 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2213 DDDEVICEIDENTIFIER2 identifier2;
2214 HRESULT hr;
2216 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2218 hr = ddraw7_GetDeviceIdentifier((IDirectDraw7 *)ddraw_from_ddraw4(iface), &identifier2, flags);
2219 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2221 return hr;
2224 /*****************************************************************************
2225 * IDirectDraw7::GetSurfaceFromDC
2227 * Returns the Surface for a GDI device context handle.
2228 * Is this related to IDirectDrawSurface::GetDC ???
2230 * Params:
2231 * hdc: hdc to return the surface for
2232 * Surface: Address to write the surface pointer to
2234 * Returns:
2235 * Always returns DD_OK because it's a stub
2237 *****************************************************************************/
2238 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc, IDirectDrawSurface7 **Surface)
2240 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
2241 IWineD3DSurface *wined3d_surface;
2242 HRESULT hr;
2244 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2246 if (!Surface) return E_INVALIDARG;
2248 hr = IWineD3DDevice_GetSurfaceFromDC(This->wineD3DDevice, hdc, &wined3d_surface);
2249 if (FAILED(hr))
2251 TRACE("No surface found for dc %p.\n", hdc);
2252 *Surface = NULL;
2253 return DDERR_NOTFOUND;
2256 *Surface = IWineD3DSurface_GetParent(wined3d_surface);
2257 IDirectDrawSurface7_AddRef(*Surface);
2258 TRACE("Returning surface %p.\n", Surface);
2259 return DD_OK;
2262 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc, IDirectDrawSurface4 **surface)
2264 IDirectDrawSurface7 *surface7;
2265 HRESULT hr;
2267 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2269 if (!surface) return E_INVALIDARG;
2271 hr = ddraw7_GetSurfaceFromDC((IDirectDraw7 *)ddraw_from_ddraw4(iface), dc, &surface7);
2272 *surface = surface7 ? (IDirectDrawSurface4 *)&((IDirectDrawSurfaceImpl *)surface7)->IDirectDrawSurface3_vtbl : NULL;
2274 return hr;
2277 static HRESULT WINAPI ddraw3_GetSurfaceFromDC(IDirectDraw3 *iface, HDC dc, IDirectDrawSurface **surface)
2279 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2281 return ddraw7_GetSurfaceFromDC((IDirectDraw7 *)ddraw_from_ddraw3(iface),
2282 dc, (IDirectDrawSurface7 **)surface);
2285 /*****************************************************************************
2286 * IDirectDraw7::RestoreAllSurfaces
2288 * Calls the restore method of all surfaces
2290 * Params:
2292 * Returns:
2293 * Always returns DD_OK because it's a stub
2295 *****************************************************************************/
2296 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2298 FIXME("iface %p stub!\n", iface);
2300 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2301 * get their parent and call their restore method. Do not implement
2302 * it in WineD3D, as restoring a surface means re-creating the
2303 * WineD3DDSurface
2305 return DD_OK;
2308 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2310 TRACE("iface %p.\n", iface);
2312 return ddraw7_RestoreAllSurfaces((IDirectDraw7 *)ddraw_from_ddraw4(iface));
2315 /*****************************************************************************
2316 * IDirectDraw7::StartModeTest
2318 * Tests the specified video modes to update the system registry with
2319 * refresh rate information. StartModeTest starts the mode test,
2320 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2321 * isn't called within 15 seconds, the mode is failed automatically
2323 * As refresh rates are handled by the X server, I don't think this
2324 * Method is important
2326 * Params:
2327 * Modes: An array of mode specifications
2328 * NumModes: The number of modes in Modes
2329 * Flags: Some flags...
2331 * Returns:
2332 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2333 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2334 * otherwise DD_OK
2336 *****************************************************************************/
2337 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2339 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2340 iface, Modes, NumModes, Flags);
2342 /* This looks sane */
2343 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2345 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2346 * As it is not, DDERR_TESTFINISHED is returned
2347 * (hopefully that's correct
2349 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2350 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2353 return DD_OK;
2356 /*****************************************************************************
2357 * ddraw_recreate_surfaces_cb
2359 * Enumeration callback for ddraw_recreate_surface.
2360 * It re-recreates the WineD3DSurface. It's pretty straightforward
2362 *****************************************************************************/
2363 HRESULT WINAPI ddraw_recreate_surfaces_cb(IDirectDrawSurface7 *surf, DDSURFACEDESC2 *desc, void *Context)
2365 IDirectDrawSurfaceImpl *surfImpl = (IDirectDrawSurfaceImpl *)surf;
2366 IDirectDrawImpl *This = surfImpl->ddraw;
2367 IWineD3DSurface *wineD3DSurface;
2368 IWineD3DSwapChain *swapchain;
2369 void *parent;
2370 HRESULT hr;
2371 IWineD3DClipper *clipper = NULL;
2373 WINED3DSURFACE_DESC Desc;
2374 enum wined3d_format_id Format;
2375 DWORD Usage;
2376 WINED3DPOOL Pool;
2378 WINED3DMULTISAMPLE_TYPE MultiSampleType;
2379 DWORD MultiSampleQuality;
2380 UINT Width;
2381 UINT Height;
2383 TRACE("surface %p, surface_desc %p, context %p.\n",
2384 surf, desc, Context);
2386 /* For the enumeration */
2387 IDirectDrawSurface7_Release(surf);
2389 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
2391 /* Get the objects */
2392 swapchain = surfImpl->wineD3DSwapChain;
2393 surfImpl->wineD3DSwapChain = NULL;
2394 wineD3DSurface = surfImpl->WineD3DSurface;
2396 /* get the clipper */
2397 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
2399 /* Get the surface properties */
2400 IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
2402 Format = Desc.format;
2403 Usage = Desc.usage;
2404 Pool = Desc.pool;
2405 MultiSampleType = Desc.multisample_type;
2406 MultiSampleQuality = Desc.multisample_quality;
2407 Width = Desc.width;
2408 Height = Desc.height;
2410 parent = IWineD3DSurface_GetParent(wineD3DSurface);
2411 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice, Width, Height, Format, TRUE /* Lockable */,
2412 FALSE /* Discard */, surfImpl->mipmap_level, Usage, Pool, MultiSampleType, MultiSampleQuality,
2413 This->ImplType, parent, &ddraw_null_wined3d_parent_ops, &surfImpl->WineD3DSurface);
2414 if (FAILED(hr))
2416 surfImpl->WineD3DSurface = wineD3DSurface;
2417 return hr;
2420 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
2422 /* TODO: Copy the surface content, except for render targets */
2424 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
2425 * the swapchain
2427 if(swapchain) {
2428 /* The backbuffers have the swapchain set as well, but the primary
2429 * owns it and destroys it
2431 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2432 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2434 surfImpl->isRenderTarget = FALSE;
2435 } else {
2436 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
2437 TRACE("Surface released successful, next surface\n");
2438 else
2439 ERR("Something's still holding the old WineD3DSurface\n");
2442 surfImpl->ImplType = This->ImplType;
2444 if(clipper)
2446 IWineD3DClipper_Release(clipper);
2448 return DDENUMRET_OK;
2451 /*****************************************************************************
2452 * ddraw_recreate_surfaces
2454 * A function, that converts all wineD3DSurfaces to the new implementation type
2455 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
2456 * new WineD3DSurface, copies the content and releases the old surface
2458 *****************************************************************************/
2459 static HRESULT ddraw_recreate_surfaces(IDirectDrawImpl *This)
2461 DDSURFACEDESC2 desc;
2462 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
2464 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
2466 /* Should happen almost never */
2467 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
2468 /* Shutdown d3d */
2469 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroySwapChain);
2471 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
2473 memset(&desc, 0, sizeof(desc));
2474 desc.dwSize = sizeof(desc);
2476 return IDirectDraw7_EnumSurfaces((IDirectDraw7 *)This, 0, &desc, This, ddraw_recreate_surfaces_cb);
2479 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain)
2481 IUnknown *swapChainParent;
2483 TRACE("swapchain %p.\n", pSwapChain);
2485 swapChainParent = IWineD3DSwapChain_GetParent(pSwapChain);
2486 return IUnknown_Release(swapChainParent);
2489 /*****************************************************************************
2490 * ddraw_create_surface
2492 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2493 * with the passed parameters.
2495 * Params:
2496 * DDSD: Description of the surface to create
2497 * Surf: Address to store the interface pointer at
2499 * Returns:
2500 * DD_OK on success
2502 *****************************************************************************/
2503 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2504 IDirectDrawSurfaceImpl **ppSurf, UINT level)
2506 WINED3DSURFTYPE ImplType = This->ImplType;
2507 HRESULT hr;
2509 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2510 This, pDDSD, ppSurf, level);
2512 if (TRACE_ON(ddraw))
2514 TRACE(" (%p) Requesting surface desc :\n", This);
2515 DDRAW_dump_surface_desc(pDDSD);
2518 /* Select the surface type, if it wasn't choosen yet */
2519 if(ImplType == SURFACE_UNKNOWN)
2521 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
2522 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2524 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
2525 ImplType = SURFACE_OPENGL;
2528 /* Otherwise use GDI surfaces for now */
2529 else
2531 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
2532 ImplType = SURFACE_GDI;
2535 /* Policy if all surface implementations are available:
2536 * First, check if a default type was set with winecfg. If not,
2537 * try Xrender surfaces, and use them if they work. Next, check if
2538 * accelerated OpenGL is available, and use GL surfaces in this
2539 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
2540 * was created, always use OpenGL surfaces.
2542 * (Note: Xrender surfaces are not implemented for now, the
2543 * unaccelerated implementation uses GDI to render in Software)
2546 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
2547 * be re-created. This could be done with IDirectDrawSurface7::Restore
2549 This->ImplType = ImplType;
2551 else
2553 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2554 && (This->ImplType != SURFACE_OPENGL)
2555 && DefaultSurfaceType == SURFACE_UNKNOWN)
2557 /* We have to change to OpenGL,
2558 * and re-create all WineD3DSurfaces
2560 ImplType = SURFACE_OPENGL;
2561 This->ImplType = ImplType;
2562 TRACE("(%p) Re-creating all surfaces\n", This);
2563 ddraw_recreate_surfaces(This);
2564 TRACE("(%p) Done recreating all surfaces\n", This);
2566 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2568 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2569 /* Do not fail surface creation, only fail 3D device creation */
2573 /* Create the Surface object */
2574 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2575 if(!*ppSurf)
2577 ERR("(%p) Error allocating memory for a surface\n", This);
2578 return DDERR_OUTOFVIDEOMEMORY;
2581 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, ImplType);
2582 if (FAILED(hr))
2584 WARN("Failed to initialize surface, hr %#x.\n", hr);
2585 HeapFree(GetProcessHeap(), 0, *ppSurf);
2586 return hr;
2589 /* Increase the surface counter, and attach the surface */
2590 InterlockedIncrement(&This->surfaces);
2591 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2593 TRACE("Created surface %p.\n", *ppSurf);
2595 return DD_OK;
2597 /*****************************************************************************
2598 * CreateAdditionalSurfaces
2600 * Creates a new mipmap chain.
2602 * Params:
2603 * root: Root surface to attach the newly created chain to
2604 * count: number of surfaces to create
2605 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2606 * effects on the caller
2607 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2608 * creates an additional surface without the mipmapping flags
2610 *****************************************************************************/
2611 static HRESULT
2612 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2613 IDirectDrawSurfaceImpl *root,
2614 UINT count,
2615 DDSURFACEDESC2 DDSD,
2616 BOOL CubeFaceRoot)
2618 UINT i, j, level = 0;
2619 HRESULT hr;
2620 IDirectDrawSurfaceImpl *last = root;
2622 for(i = 0; i < count; i++)
2624 IDirectDrawSurfaceImpl *object2 = NULL;
2626 /* increase the mipmap level, but only if a mipmap is created
2627 * In this case, also halve the size
2629 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2631 level++;
2632 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2633 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2634 /* Set the mipmap sublevel flag according to msdn */
2635 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2637 else
2639 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2641 CubeFaceRoot = FALSE;
2643 hr = ddraw_create_surface(This, &DDSD, &object2, level);
2644 if(hr != DD_OK)
2646 return hr;
2649 /* Add the new surface to the complex attachment array */
2650 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2652 if(last->complex_array[j]) continue;
2653 last->complex_array[j] = object2;
2654 break;
2656 last = object2;
2658 /* Remove the (possible) back buffer cap from the new surface description,
2659 * because only one surface in the flipping chain is a back buffer, one
2660 * is a front buffer, the others are just primary surfaces.
2662 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2664 return DD_OK;
2667 /* Must set all attached surfaces (e.g. mipmaps) versions as well */
2668 static void ddraw_set_surface_version(IDirectDrawSurfaceImpl *surface, UINT version)
2670 unsigned int i;
2672 TRACE("surface %p, version %u -> %u.\n", surface, surface->version, version);
2674 surface->version = version;
2675 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
2677 if (!surface->complex_array[i]) break;
2678 ddraw_set_surface_version(surface->complex_array[i], version);
2680 while ((surface = surface->next_attached))
2682 ddraw_set_surface_version(surface, version);
2686 /*****************************************************************************
2687 * ddraw_attach_d3d_device
2689 * Initializes the D3D capabilities of WineD3D
2691 * Params:
2692 * primary: The primary surface for D3D
2694 * Returns
2695 * DD_OK on success,
2696 * DDERR_* otherwise
2698 *****************************************************************************/
2699 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2701 WINED3DPRESENT_PARAMETERS localParameters;
2702 HWND window = ddraw->dest_window;
2703 HRESULT hr;
2705 TRACE("ddraw %p, primary %p.\n", ddraw, primary);
2707 if (!window || window == GetDesktopWindow())
2709 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
2710 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
2711 NULL, NULL, NULL, NULL);
2712 if (!window)
2714 ERR("Failed to create window, last error %#x.\n", GetLastError());
2715 return E_FAIL;
2718 ShowWindow(window, SW_HIDE); /* Just to be sure */
2719 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
2721 else
2723 TRACE("Using existing window %p for Direct3D rendering.\n", window);
2725 ddraw->d3d_window = window;
2727 /* Store the future Render Target surface */
2728 ddraw->d3d_target = primary;
2730 /* Use the surface description for the device parameters, not the device
2731 * settings. The application might render to an offscreen surface. */
2732 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
2733 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
2734 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2735 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2736 ? primary->surface_desc.dwBackBufferCount : 0;
2737 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2738 localParameters.MultiSampleQuality = 0;
2739 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
2740 localParameters.hDeviceWindow = window;
2741 localParameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2742 localParameters.EnableAutoDepthStencil = TRUE;
2743 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16_UNORM;
2744 localParameters.Flags = 0;
2745 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2746 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2748 /* Set this NOW, otherwise creating the depth stencil surface will cause a
2749 * recursive loop until ram or emulated video memory is full. */
2750 ddraw->d3d_initialized = TRUE;
2751 hr = IWineD3DDevice_Init3D(ddraw->wineD3DDevice, &localParameters);
2752 if (FAILED(hr))
2754 ddraw->d3d_target = NULL;
2755 ddraw->d3d_initialized = FALSE;
2756 return hr;
2759 ddraw->declArraySize = 2;
2760 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
2761 if (!ddraw->decls)
2763 ERR("Error allocating an array for the converted vertex decls.\n");
2764 ddraw->declArraySize = 0;
2765 hr = IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
2766 return E_OUTOFMEMORY;
2769 TRACE("Successfully initialized 3D.\n");
2771 return DD_OK;
2774 static HRESULT ddraw_create_gdi_swapchain(IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *primary)
2776 WINED3DPRESENT_PARAMETERS presentation_parameters;
2777 HWND window;
2778 HRESULT hr;
2780 window = ddraw->dest_window;
2782 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
2784 /* Use the surface description for the device parameters, not the device
2785 * settings. The application might render to an offscreen surface. */
2786 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
2787 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
2788 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
2789 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT)
2790 ? primary->surface_desc.dwBackBufferCount : 0;
2791 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
2792 presentation_parameters.MultiSampleQuality = 0;
2793 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
2794 presentation_parameters.hDeviceWindow = window;
2795 presentation_parameters.Windowed = !(ddraw->cooperative_level & DDSCL_FULLSCREEN);
2796 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
2797 presentation_parameters.AutoDepthStencilFormat = 0;
2798 presentation_parameters.Flags = 0;
2799 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT;
2800 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
2802 ddraw->d3d_target = primary;
2803 hr = IWineD3DDevice_InitGDI(ddraw->wineD3DDevice, &presentation_parameters);
2804 ddraw->d3d_target = NULL;
2805 if (FAILED(hr))
2807 WARN("Failed to initialize GDI ddraw implementation, hr %#x.\n", hr);
2808 primary->wineD3DSwapChain = NULL;
2811 return hr;
2814 /*****************************************************************************
2815 * IDirectDraw7::CreateSurface
2817 * Creates a new IDirectDrawSurface object and returns its interface.
2819 * The surface connections with wined3d are a bit tricky. Basically it works
2820 * like this:
2822 * |------------------------| |-----------------|
2823 * | DDraw surface | | WineD3DSurface |
2824 * | | | |
2825 * | WineD3DSurface |-------------->| |
2826 * | Child |<------------->| Parent |
2827 * |------------------------| |-----------------|
2829 * The DDraw surface is the parent of the wined3d surface, and it releases
2830 * the WineD3DSurface when the ddraw surface is destroyed.
2832 * However, for all surfaces which can be in a container in WineD3D,
2833 * we have to do this. These surfaces are usually complex surfaces,
2834 * so this concerns primary surfaces with a front and a back buffer,
2835 * and textures.
2837 * |------------------------| |-----------------|
2838 * | DDraw surface | | Container |
2839 * | | | |
2840 * | Child |<------------->| Parent |
2841 * | Texture |<------------->| |
2842 * | WineD3DSurface |<----| | Levels |<--|
2843 * | Complex connection | | | | |
2844 * |------------------------| | |-----------------| |
2845 * ^ | |
2846 * | | |
2847 * | | |
2848 * | |------------------| | |-----------------| |
2849 * | | IParent | |-------->| WineD3DSurface | |
2850 * | | | | | |
2851 * | | Child |<------------->| Parent | |
2852 * | | | | Container |<--|
2853 * | |------------------| |-----------------| |
2854 * | |
2855 * | |----------------------| |
2856 * | | DDraw surface 2 | |
2857 * | | | |
2858 * |<->| Complex root Child | |
2859 * | | Texture | |
2860 * | | WineD3DSurface |<----| |
2861 * | |----------------------| | |
2862 * | | |
2863 * | |---------------------| | |-----------------| |
2864 * | | IParent | |----->| WineD3DSurface | |
2865 * | | | | | |
2866 * | | Child |<---------->| Parent | |
2867 * | |---------------------| | Container |<--|
2868 * | |-----------------| |
2869 * | |
2870 * | ---More surfaces can follow--- |
2872 * The reason is that the IWineD3DSwapchain(render target container)
2873 * and the IWineD3DTexure(Texture container) release the parents
2874 * of their surface's children, but by releasing the complex root
2875 * the surfaces which are complexly attached to it are destroyed
2876 * too. See IDirectDrawSurface::Release for a more detailed
2877 * explanation.
2879 * Params:
2880 * DDSD: Description of the surface to create
2881 * Surf: Address to store the interface pointer at
2882 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2883 * aggregation, so it has to be NULL
2885 * Returns:
2886 * DD_OK on success
2887 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2888 * DDERR_* if an error occurs
2890 *****************************************************************************/
2891 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
2892 IDirectDrawSurface7 **Surf, IUnknown *UnkOuter)
2894 IDirectDrawSurfaceImpl *object = NULL;
2895 HRESULT hr;
2896 LONG extra_surfaces = 0;
2897 DDSURFACEDESC2 desc2;
2898 WINED3DDISPLAYMODE Mode;
2899 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2901 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
2903 /* Some checks before we start */
2904 if (TRACE_ON(ddraw))
2906 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2907 DDRAW_dump_surface_desc(DDSD);
2909 EnterCriticalSection(&ddraw_cs);
2911 if (UnkOuter != NULL)
2913 FIXME("(%p) : outer != NULL?\n", ddraw);
2914 LeaveCriticalSection(&ddraw_cs);
2915 return CLASS_E_NOAGGREGATION; /* unchecked */
2918 if (Surf == NULL)
2920 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2921 LeaveCriticalSection(&ddraw_cs);
2922 return E_POINTER; /* unchecked */
2925 if (!(DDSD->dwFlags & DDSD_CAPS))
2927 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2928 DDSD->dwFlags |= DDSD_CAPS;
2931 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2933 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2934 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2937 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2939 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2940 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2941 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2944 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2945 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2947 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2948 ddraw);
2949 *Surf = NULL;
2950 LeaveCriticalSection(&ddraw_cs);
2951 return DDERR_NOEXCLUSIVEMODE;
2954 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2956 WARN("Application wanted to create back buffer primary surface\n");
2957 LeaveCriticalSection(&ddraw_cs);
2958 return DDERR_INVALIDCAPS;
2961 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2963 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2964 WARN("Application tries to put the surface in both system and video memory\n");
2965 LeaveCriticalSection(&ddraw_cs);
2966 *Surf = NULL;
2967 return DDERR_INVALIDCAPS;
2970 /* Check cube maps but only if the size includes them */
2971 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2973 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2974 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2976 WARN("Cube map faces requested without cube map flag\n");
2977 LeaveCriticalSection(&ddraw_cs);
2978 return DDERR_INVALIDCAPS;
2980 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2981 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2983 WARN("Cube map without faces requested\n");
2984 LeaveCriticalSection(&ddraw_cs);
2985 return DDERR_INVALIDPARAMS;
2988 /* Quick tests confirm those can be created, but we don't do that yet */
2989 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2990 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2992 FIXME("Partial cube maps not supported yet\n");
2996 /* According to the msdn this flag is ignored by CreateSurface */
2997 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2998 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
3000 /* Modify some flags */
3001 memset(&desc2, 0, sizeof(desc2));
3002 desc2.dwSize = sizeof(desc2); /* For the struct copy */
3003 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
3004 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
3005 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
3007 /* Get the video mode from WineD3D - we will need it */
3008 hr = IWineD3DDevice_GetDisplayMode(ddraw->wineD3DDevice, 0, &Mode);
3009 if(FAILED(hr))
3011 ERR("Failed to read display mode from wined3d\n");
3012 switch(ddraw->orig_bpp)
3014 case 8:
3015 Mode.Format = WINED3DFMT_P8_UINT;
3016 break;
3018 case 15:
3019 Mode.Format = WINED3DFMT_B5G5R5X1_UNORM;
3020 break;
3022 case 16:
3023 Mode.Format = WINED3DFMT_B5G6R5_UNORM;
3024 break;
3026 case 24:
3027 Mode.Format = WINED3DFMT_B8G8R8_UNORM;
3028 break;
3030 case 32:
3031 Mode.Format = WINED3DFMT_B8G8R8X8_UNORM;
3032 break;
3034 Mode.Width = ddraw->orig_width;
3035 Mode.Height = ddraw->orig_height;
3038 /* No pixelformat given? Use the current screen format */
3039 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
3041 desc2.dwFlags |= DDSD_PIXELFORMAT;
3042 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
3044 /* Wait: It could be a Z buffer */
3045 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3047 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
3049 case 15:
3050 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_S1_UINT_D15_UNORM);
3051 break;
3052 case 16:
3053 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16_UNORM);
3054 break;
3055 case 24:
3056 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_X8D24_UNORM);
3057 break;
3058 case 32:
3059 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32_UNORM);
3060 break;
3061 default:
3062 ERR("Unknown Z buffer bit depth\n");
3065 else
3067 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
3071 /* No Width or no Height? Use the original screen size
3073 if(!(desc2.dwFlags & DDSD_WIDTH) ||
3074 !(desc2.dwFlags & DDSD_HEIGHT) )
3076 /* Invalid for non-render targets */
3077 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3079 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
3080 *Surf = NULL;
3081 LeaveCriticalSection(&ddraw_cs);
3082 return DDERR_INVALIDPARAMS;
3085 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
3086 desc2.dwWidth = Mode.Width;
3087 desc2.dwHeight = Mode.Height;
3090 if (!desc2.dwWidth || !desc2.dwHeight)
3092 LeaveCriticalSection(&ddraw_cs);
3093 return DDERR_INVALIDPARAMS;
3096 /* Mipmap count fixes */
3097 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3099 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
3101 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
3103 /* Mipmap count is given, should not be 0 */
3104 if( desc2.u2.dwMipMapCount == 0 )
3106 LeaveCriticalSection(&ddraw_cs);
3107 return DDERR_INVALIDPARAMS;
3110 else
3112 /* Undocumented feature: Create sublevels until
3113 * either the width or the height is 1
3115 DWORD min = desc2.dwWidth < desc2.dwHeight ?
3116 desc2.dwWidth : desc2.dwHeight;
3117 desc2.u2.dwMipMapCount = 0;
3118 while( min )
3120 desc2.u2.dwMipMapCount += 1;
3121 min >>= 1;
3125 else
3127 /* Not-complex mipmap -> Mipmapcount = 1 */
3128 desc2.u2.dwMipMapCount = 1;
3130 extra_surfaces = desc2.u2.dwMipMapCount - 1;
3132 /* There's a mipmap count in the created surface in any case */
3133 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
3135 /* If no mipmap is given, the texture has only one level */
3137 /* The first surface is a front buffer, the back buffer is created afterwards */
3138 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
3140 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
3143 /* The root surface in a cube map is positive x */
3144 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3146 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3147 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3150 /* Create the first surface */
3151 hr = ddraw_create_surface(ddraw, &desc2, &object, 0);
3152 if (FAILED(hr))
3154 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
3155 LeaveCriticalSection(&ddraw_cs);
3156 return hr;
3158 object->is_complex_root = TRUE;
3160 *Surf = (IDirectDrawSurface7 *)object;
3162 /* Create Additional surfaces if necessary
3163 * This applies to Primary surfaces which have a back buffer count
3164 * set, but not to mipmap textures. In case of Mipmap textures,
3165 * wineD3D takes care of the creation of additional surfaces
3167 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
3169 extra_surfaces = DDSD->dwBackBufferCount;
3170 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
3171 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
3172 desc2.dwBackBufferCount = 0;
3175 hr = DD_OK;
3176 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3178 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
3179 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
3180 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3181 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
3182 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
3183 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3184 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
3185 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
3186 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3187 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
3188 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
3189 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3190 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
3191 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
3192 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE);
3193 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
3194 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
3197 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE);
3198 if(hr != DD_OK)
3200 /* This destroys and possibly created surfaces too */
3201 IDirectDrawSurface_Release((IDirectDrawSurface7 *)object);
3202 LeaveCriticalSection(&ddraw_cs);
3203 return hr;
3206 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
3207 * But attach the d3ddevice only if the currently created surface was
3208 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
3209 * The only case I can think of where this doesn't apply is when a
3210 * 2D app was configured by the user to run with OpenGL and it didn't create
3211 * the render target as first surface. In this case the render target creation
3212 * will cause the 3D init.
3214 if( (ddraw->ImplType == SURFACE_OPENGL) && !(ddraw->d3d_initialized) &&
3215 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
3217 IDirectDrawSurfaceImpl *target = object, *surface;
3218 struct list *entry;
3220 /* Search for the primary to use as render target */
3221 LIST_FOR_EACH(entry, &ddraw->surface_list)
3223 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3224 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
3225 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
3227 /* found */
3228 target = surface;
3229 TRACE("Using primary %p as render target\n", target);
3230 break;
3234 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", ddraw, target);
3235 hr = ddraw_attach_d3d_device(ddraw, target);
3236 if (hr != D3D_OK)
3238 IDirectDrawSurfaceImpl *release_surf;
3239 ERR("ddraw_attach_d3d_device failed, hr %#x\n", hr);
3240 *Surf = NULL;
3242 /* The before created surface structures are in an incomplete state here.
3243 * WineD3D holds the reference on the IParents, and it released them on the failure
3244 * already. So the regular release method implementation would fail on the attempt
3245 * to destroy either the IParents or the swapchain. So free the surface here.
3246 * The surface structure here is a list, not a tree, because onscreen targets
3247 * cannot be cube textures
3249 while(object)
3251 release_surf = object;
3252 object = object->complex_array[0];
3253 ddraw_surface_destroy(release_surf);
3255 LeaveCriticalSection(&ddraw_cs);
3256 return hr;
3259 else if(!(ddraw->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3261 ddraw_create_gdi_swapchain(ddraw, object);
3264 /* Addref the ddraw interface to keep an reference for each surface */
3265 IDirectDraw7_AddRef((IDirectDraw7 *)ddraw);
3266 object->ifaceToRelease = (IUnknown *)ddraw;
3268 /* Create a WineD3DTexture if a texture was requested */
3269 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3271 enum wined3d_format_id Format;
3272 UINT levels;
3273 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
3275 ddraw->tex_root = object;
3277 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3279 /* a mipmap is created, create enough levels */
3280 levels = desc2.u2.dwMipMapCount;
3282 else
3284 /* No mipmap is created, create one level */
3285 levels = 1;
3288 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
3289 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3291 Pool = WINED3DPOOL_SYSTEMMEM;
3293 /* Should I forward the MANAGED cap to the managed pool ? */
3295 /* Get the format. It's set already by CreateNewSurface */
3296 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
3298 /* The surfaces are already created, the callback only
3299 * passes the IWineD3DSurface to WineD3D
3301 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3303 hr = IWineD3DDevice_CreateCubeTexture(ddraw->wineD3DDevice, DDSD->dwWidth, levels, 0,
3304 Format, Pool, object, &ddraw_null_wined3d_parent_ops,
3305 (IWineD3DCubeTexture **)&object->wineD3DTexture);
3307 else
3309 hr = IWineD3DDevice_CreateTexture(ddraw->wineD3DDevice, DDSD->dwWidth, DDSD->dwHeight,
3310 levels, 0, Format, Pool, object, &ddraw_null_wined3d_parent_ops,
3311 (IWineD3DTexture **)&object->wineD3DTexture);
3313 ddraw->tex_root = NULL;
3316 LeaveCriticalSection(&ddraw_cs);
3317 return hr;
3320 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface,
3321 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3323 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3325 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3326 iface, surface_desc, surface, outer_unknown);
3328 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3330 WARN("Application supplied invalid surface descriptor\n");
3331 return DDERR_INVALIDPARAMS;
3334 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3336 if (TRACE_ON(ddraw))
3338 TRACE(" (%p) Requesting surface desc :\n", iface);
3339 DDRAW_dump_surface_desc(surface_desc);
3342 WARN("Application tried to create an explicit front or back buffer\n");
3343 return DDERR_INVALIDCAPS;
3346 return CreateSurface(This, surface_desc, surface, outer_unknown);
3349 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3350 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3352 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
3353 IDirectDrawSurfaceImpl *impl;
3354 HRESULT hr;
3356 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3357 iface, surface_desc, surface, outer_unknown);
3359 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3361 WARN("Application supplied invalid surface descriptor\n");
3362 return DDERR_INVALIDPARAMS;
3365 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3367 if (TRACE_ON(ddraw))
3369 TRACE(" (%p) Requesting surface desc :\n", iface);
3370 DDRAW_dump_surface_desc(surface_desc);
3373 WARN("Application tried to create an explicit front or back buffer\n");
3374 return DDERR_INVALIDCAPS;
3377 hr = CreateSurface(ddraw, surface_desc, (IDirectDrawSurface7 **)surface, outer_unknown);
3378 impl = (IDirectDrawSurfaceImpl *)*surface;
3379 if (SUCCEEDED(hr) && impl)
3381 ddraw_set_surface_version(impl, 4);
3382 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3383 IDirectDraw4_AddRef(iface);
3384 impl->ifaceToRelease = (IUnknown *)iface;
3387 return hr;
3390 static HRESULT WINAPI ddraw3_CreateSurface(IDirectDraw3 *iface,
3391 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3393 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
3394 IDirectDrawSurface7 *surface7;
3395 IDirectDrawSurfaceImpl *impl;
3396 HRESULT hr;
3398 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3399 iface, surface_desc, surface, outer_unknown);
3401 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3403 WARN("Application supplied invalid surface descriptor\n");
3404 return DDERR_INVALIDPARAMS;
3407 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3409 if (TRACE_ON(ddraw))
3411 TRACE(" (%p) Requesting surface desc :\n", iface);
3412 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3415 WARN("Application tried to create an explicit front or back buffer\n");
3416 return DDERR_INVALIDCAPS;
3419 hr = CreateSurface(ddraw, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3420 if (FAILED(hr))
3422 *surface = NULL;
3423 return hr;
3426 impl = (IDirectDrawSurfaceImpl *)surface7;
3427 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3428 ddraw_set_surface_version(impl, 3);
3429 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3430 IDirectDraw3_AddRef(iface);
3431 impl->ifaceToRelease = (IUnknown *)iface;
3433 return hr;
3436 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3437 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3439 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
3440 IDirectDrawSurface7 *surface7;
3441 IDirectDrawSurfaceImpl *impl;
3442 HRESULT hr;
3444 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3445 iface, surface_desc, surface, outer_unknown);
3447 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3449 WARN("Application supplied invalid surface descriptor\n");
3450 return DDERR_INVALIDPARAMS;
3453 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3455 if (TRACE_ON(ddraw))
3457 TRACE(" (%p) Requesting surface desc :\n", iface);
3458 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3461 WARN("Application tried to create an explicit front or back buffer\n");
3462 return DDERR_INVALIDCAPS;
3465 hr = CreateSurface(ddraw, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3466 if (FAILED(hr))
3468 *surface = NULL;
3469 return hr;
3472 impl = (IDirectDrawSurfaceImpl *)surface7;
3473 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3474 ddraw_set_surface_version(impl, 2);
3475 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3476 impl->ifaceToRelease = NULL;
3478 return hr;
3481 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3482 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3484 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
3485 IDirectDrawSurface7 *surface7;
3486 IDirectDrawSurfaceImpl *impl;
3487 HRESULT hr;
3489 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3490 iface, surface_desc, surface, outer_unknown);
3492 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3494 WARN("Application supplied invalid surface descriptor\n");
3495 return DDERR_INVALIDPARAMS;
3498 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3499 * primaries anyway. */
3500 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3501 hr = CreateSurface(ddraw, (DDSURFACEDESC2 *)surface_desc, &surface7, outer_unknown);
3502 if (FAILED(hr))
3504 *surface = NULL;
3505 return hr;
3508 impl = (IDirectDrawSurfaceImpl *)surface7;
3509 *surface = (IDirectDrawSurface *)&impl->IDirectDrawSurface3_vtbl;
3510 ddraw_set_surface_version(impl, 1);
3511 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3512 impl->ifaceToRelease = NULL;
3514 return hr;
3517 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3518 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3520 static BOOL
3521 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3522 const DDPIXELFORMAT *provided)
3524 /* Some flags must be present in both or neither for a match. */
3525 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3526 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3527 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3529 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3530 return FALSE;
3532 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3533 return FALSE;
3535 if (requested->dwFlags & DDPF_FOURCC)
3536 if (requested->dwFourCC != provided->dwFourCC)
3537 return FALSE;
3539 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3540 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3541 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3542 return FALSE;
3544 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3545 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3546 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3547 return FALSE;
3549 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3550 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3551 return FALSE;
3553 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3554 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3555 |DDPF_BUMPDUDV))
3556 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3557 return FALSE;
3559 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3560 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3561 return FALSE;
3563 return TRUE;
3566 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3568 struct compare_info
3570 DWORD flag;
3571 ptrdiff_t offset;
3572 size_t size;
3575 #define CMP(FLAG, FIELD) \
3576 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3577 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3579 static const struct compare_info compare[] =
3581 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3582 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3583 CMP(CAPS, ddsCaps),
3584 CMP(CKDESTBLT, ddckCKDestBlt),
3585 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3586 CMP(CKSRCBLT, ddckCKSrcBlt),
3587 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3588 CMP(HEIGHT, dwHeight),
3589 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3590 CMP(LPSURFACE, lpSurface),
3591 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3592 CMP(PITCH, u1 /* lPitch */),
3593 /* PIXELFORMAT: manual */
3594 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3595 CMP(TEXTURESTAGE, dwTextureStage),
3596 CMP(WIDTH, dwWidth),
3597 /* ZBUFFERBITDEPTH: "obsolete" */
3600 #undef CMP
3602 unsigned int i;
3604 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3605 return FALSE;
3607 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3609 if (requested->dwFlags & compare[i].flag
3610 && memcmp((const char *)provided + compare[i].offset,
3611 (const char *)requested + compare[i].offset,
3612 compare[i].size) != 0)
3613 return FALSE;
3616 if (requested->dwFlags & DDSD_PIXELFORMAT)
3618 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3619 &provided->u4.ddpfPixelFormat))
3620 return FALSE;
3623 return TRUE;
3626 #undef DDENUMSURFACES_SEARCHTYPE
3627 #undef DDENUMSURFACES_MATCHTYPE
3629 struct surfacescallback_context
3631 LPDDENUMSURFACESCALLBACK func;
3632 void *context;
3635 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3636 DDSURFACEDESC2 *surface_desc, void *context)
3638 struct surfacescallback_context *cbcontext = context;
3640 return cbcontext->func((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
3641 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3644 /*****************************************************************************
3645 * IDirectDraw7::EnumSurfaces
3647 * Loops through all surfaces attached to this device and calls the
3648 * application callback. This can't be relayed to WineD3DDevice,
3649 * because some WineD3DSurfaces' parents are IParent objects
3651 * Params:
3652 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3653 * DDSD: Description to filter for
3654 * Context: Application-provided pointer, it's passed unmodified to the
3655 * Callback function
3656 * Callback: Address to call for each surface
3658 * Returns:
3659 * DDERR_INVALIDPARAMS if the callback is NULL
3660 * DD_OK on success
3662 *****************************************************************************/
3663 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3664 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3666 /* The surface enumeration is handled by WineDDraw,
3667 * because it keeps track of all surfaces attached to
3668 * it. The filtering is done by our callback function,
3669 * because WineDDraw doesn't handle ddraw-like surface
3670 * caps structures
3672 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3673 IDirectDrawSurfaceImpl *surf;
3674 BOOL all, nomatch;
3675 DDSURFACEDESC2 desc;
3676 struct list *entry, *entry2;
3678 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3679 iface, Flags, DDSD, Context, Callback);
3681 all = Flags & DDENUMSURFACES_ALL;
3682 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3684 EnterCriticalSection(&ddraw_cs);
3686 if(!Callback)
3688 LeaveCriticalSection(&ddraw_cs);
3689 return DDERR_INVALIDPARAMS;
3692 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3693 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3695 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3696 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3698 desc = surf->surface_desc;
3699 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
3700 if (Callback((IDirectDrawSurface7 *)surf, &desc, Context) != DDENUMRET_OK)
3702 LeaveCriticalSection(&ddraw_cs);
3703 return DD_OK;
3707 LeaveCriticalSection(&ddraw_cs);
3708 return DD_OK;
3711 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3712 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3714 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3715 iface, flags, surface_desc, context, callback);
3717 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw4(iface),
3718 flags, surface_desc, context, (LPDDENUMSURFACESCALLBACK7)callback);
3721 static HRESULT WINAPI ddraw3_EnumSurfaces(IDirectDraw3 *iface, DWORD flags,
3722 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3724 struct surfacescallback_context cbcontext;
3726 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3727 iface, flags, surface_desc, context, callback);
3729 cbcontext.func = callback;
3730 cbcontext.context = context;
3732 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags,
3733 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumSurfacesCallbackThunk);
3736 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3737 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3739 struct surfacescallback_context cbcontext;
3741 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3742 iface, flags, surface_desc, context, callback);
3744 cbcontext.func = callback;
3745 cbcontext.context = context;
3747 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags,
3748 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumSurfacesCallbackThunk);
3751 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3752 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3754 struct surfacescallback_context cbcontext;
3756 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3757 iface, flags, surface_desc, context, callback);
3759 cbcontext.func = callback;
3760 cbcontext.context = context;
3762 return ddraw7_EnumSurfaces((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags,
3763 (DDSURFACEDESC2 *)surface_desc, &cbcontext, EnumSurfacesCallbackThunk);
3766 /*****************************************************************************
3767 * DirectDrawCreateClipper (DDRAW.@)
3769 * Creates a new IDirectDrawClipper object.
3771 * Params:
3772 * Clipper: Address to write the interface pointer to
3773 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3774 * NULL
3776 * Returns:
3777 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3778 * E_OUTOFMEMORY if allocating the object failed
3780 *****************************************************************************/
3781 HRESULT WINAPI
3782 DirectDrawCreateClipper(DWORD Flags,
3783 LPDIRECTDRAWCLIPPER *Clipper,
3784 IUnknown *UnkOuter)
3786 IDirectDrawClipperImpl* object;
3787 HRESULT hr;
3789 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3790 Flags, Clipper, UnkOuter);
3792 EnterCriticalSection(&ddraw_cs);
3793 if (UnkOuter != NULL)
3795 LeaveCriticalSection(&ddraw_cs);
3796 return CLASS_E_NOAGGREGATION;
3799 if (!LoadWineD3D())
3801 LeaveCriticalSection(&ddraw_cs);
3802 return DDERR_NODIRECTDRAWSUPPORT;
3805 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3806 sizeof(IDirectDrawClipperImpl));
3807 if (object == NULL)
3809 LeaveCriticalSection(&ddraw_cs);
3810 return E_OUTOFMEMORY;
3813 hr = ddraw_clipper_init(object);
3814 if (FAILED(hr))
3816 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3817 HeapFree(GetProcessHeap(), 0, object);
3818 LeaveCriticalSection(&ddraw_cs);
3819 return hr;
3822 TRACE("Created clipper %p.\n", object);
3823 *Clipper = (IDirectDrawClipper *) object;
3824 LeaveCriticalSection(&ddraw_cs);
3825 return DD_OK;
3828 /*****************************************************************************
3829 * IDirectDraw7::CreateClipper
3831 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3833 *****************************************************************************/
3834 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3835 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3837 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3838 iface, Flags, Clipper, UnkOuter);
3840 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3843 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface,
3844 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3846 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3847 iface, flags, clipper, outer_unknown);
3849 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw4(iface), flags, clipper, outer_unknown);
3852 static HRESULT WINAPI ddraw3_CreateClipper(IDirectDraw3 *iface,
3853 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3855 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3856 iface, flags, clipper, outer_unknown);
3858 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw3(iface), flags, clipper, outer_unknown);
3861 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3862 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3864 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3865 iface, flags, clipper, outer_unknown);
3867 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw2(iface), flags, clipper, outer_unknown);
3870 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3871 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3873 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3874 iface, flags, clipper, outer_unknown);
3876 return ddraw7_CreateClipper((IDirectDraw7 *)ddraw_from_ddraw1(iface), flags, clipper, outer_unknown);
3879 /*****************************************************************************
3880 * IDirectDraw7::CreatePalette
3882 * Creates a new IDirectDrawPalette object
3884 * Params:
3885 * Flags: The flags for the new clipper
3886 * ColorTable: Color table to assign to the new clipper
3887 * Palette: Address to write the interface pointer to
3888 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3889 * NULL
3891 * Returns:
3892 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3893 * E_OUTOFMEMORY if allocating the object failed
3895 *****************************************************************************/
3896 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3897 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3899 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
3900 IDirectDrawPaletteImpl *object;
3901 HRESULT hr;
3903 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3904 iface, Flags, ColorTable, Palette, pUnkOuter);
3906 EnterCriticalSection(&ddraw_cs);
3907 if(pUnkOuter != NULL)
3909 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3910 LeaveCriticalSection(&ddraw_cs);
3911 return CLASS_E_NOAGGREGATION;
3914 /* The refcount test shows that a cooplevel is required for this */
3915 if(!This->cooperative_level)
3917 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3918 LeaveCriticalSection(&ddraw_cs);
3919 return DDERR_NOCOOPERATIVELEVELSET;
3922 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3923 if(!object)
3925 ERR("Out of memory when allocating memory for a palette implementation\n");
3926 LeaveCriticalSection(&ddraw_cs);
3927 return E_OUTOFMEMORY;
3930 hr = ddraw_palette_init(object, This, Flags, ColorTable);
3931 if (FAILED(hr))
3933 WARN("Failed to initialize palette, hr %#x.\n", hr);
3934 HeapFree(GetProcessHeap(), 0, object);
3935 LeaveCriticalSection(&ddraw_cs);
3936 return hr;
3939 TRACE("Created palette %p.\n", object);
3940 *Palette = (IDirectDrawPalette *)object;
3941 LeaveCriticalSection(&ddraw_cs);
3942 return DD_OK;
3945 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags,
3946 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3948 IDirectDrawImpl *ddraw = ddraw_from_ddraw4(iface);
3949 HRESULT hr;
3951 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3952 iface, flags, entries, palette, outer_unknown);
3954 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3955 if (SUCCEEDED(hr) && *palette)
3957 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3958 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3959 IDirectDraw4_AddRef(iface);
3960 impl->ifaceToRelease = (IUnknown *)iface;
3962 return hr;
3965 static HRESULT WINAPI ddraw3_CreatePalette(IDirectDraw3 *iface, DWORD flags,
3966 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3968 IDirectDrawImpl *ddraw = ddraw_from_ddraw3(iface);
3969 HRESULT hr;
3971 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3972 iface, flags, entries, palette, outer_unknown);
3974 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3975 if (SUCCEEDED(hr) && *palette)
3977 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3978 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
3979 IDirectDraw4_AddRef(iface);
3980 impl->ifaceToRelease = (IUnknown *)iface;
3983 return hr;
3986 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3987 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3989 IDirectDrawImpl *ddraw = ddraw_from_ddraw2(iface);
3990 HRESULT hr;
3992 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3993 iface, flags, entries, palette, outer_unknown);
3995 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
3996 if (SUCCEEDED(hr) && *palette)
3998 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
3999 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
4000 impl->ifaceToRelease = NULL;
4003 return hr;
4006 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
4007 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
4009 IDirectDrawImpl *ddraw = ddraw_from_ddraw1(iface);
4010 HRESULT hr;
4012 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
4013 iface, flags, entries, palette, outer_unknown);
4015 hr = ddraw7_CreatePalette((IDirectDraw7 *)ddraw, flags, entries, palette, outer_unknown);
4016 if (SUCCEEDED(hr) && *palette)
4018 IDirectDrawPaletteImpl *impl = (IDirectDrawPaletteImpl *)*palette;
4019 IDirectDraw7_Release((IDirectDraw7 *)ddraw);
4020 impl->ifaceToRelease = NULL;
4023 return hr;
4026 /*****************************************************************************
4027 * IDirectDraw7::DuplicateSurface
4029 * Duplicates a surface. The surface memory points to the same memory as
4030 * the original surface, and it's released when the last surface referencing
4031 * it is released. I guess that's beyond Wine's surface management right now
4032 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
4033 * test application to implement this)
4035 * Params:
4036 * Src: Address of the source surface
4037 * Dest: Address to write the new surface pointer to
4039 * Returns:
4040 * See IDirectDraw7::CreateSurface
4042 *****************************************************************************/
4043 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
4044 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
4046 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Src;
4048 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
4050 /* For now, simply create a new, independent surface */
4051 return IDirectDraw7_CreateSurface(iface,
4052 &Surf->surface_desc,
4053 Dest,
4054 NULL);
4057 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface,
4058 IDirectDrawSurface4 *src, IDirectDrawSurface4 **dst)
4060 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4062 return ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw4(iface),
4063 (IDirectDrawSurface7 *)src, (IDirectDrawSurface7 **)dst);
4066 static HRESULT WINAPI ddraw3_DuplicateSurface(IDirectDraw3 *iface,
4067 IDirectDrawSurface *src, IDirectDrawSurface **dst)
4069 IDirectDrawSurface7 *src7, *dst7;
4070 HRESULT hr;
4072 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4073 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4074 hr = ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw3(iface), src7, &dst7);
4075 if (FAILED(hr))
4076 return hr;
4077 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4078 return hr;
4081 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
4082 IDirectDrawSurface *src, IDirectDrawSurface **dst)
4084 IDirectDrawSurface7 *src7, *dst7;
4085 HRESULT hr;
4087 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4088 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4089 hr = ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw2(iface), src7, &dst7);
4090 if (FAILED(hr))
4091 return hr;
4092 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4093 return hr;
4096 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface,
4097 IDirectDrawSurface *src, IDirectDrawSurface **dst)
4099 IDirectDrawSurface7 *src7, *dst7;
4100 HRESULT hr;
4102 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
4103 src7 = (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)src);
4104 hr = ddraw7_DuplicateSurface((IDirectDraw7 *)ddraw_from_ddraw1(iface), src7, &dst7);
4105 if (FAILED(hr))
4106 return hr;
4107 *dst = (IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)dst7)->IDirectDrawSurface3_vtbl;
4108 return hr;
4111 /*****************************************************************************
4112 * IDirect3D7::EnumDevices
4114 * The EnumDevices method for IDirect3D7. It enumerates all supported
4115 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
4117 * Params:
4118 * callback: Function to call for each enumerated device
4119 * context: Pointer to pass back to the app
4121 * Returns:
4122 * D3D_OK, or the return value of the GetCaps call
4124 *****************************************************************************/
4125 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
4127 char interface_name_tnl[] = "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D";
4128 char device_name_tnl[] = "Wine D3D7 T&L HAL";
4129 char interface_name_hal[] = "WINE Direct3D7 Hardware acceleration using WineD3D";
4130 char device_name_hal[] = "Wine D3D7 HAL";
4131 char interface_name_rgb[] = "WINE Direct3D7 RGB Software Emulation using WineD3D";
4132 char device_name_rgb[] = "Wine D3D7 RGB";
4134 IDirectDrawImpl *ddraw = ddraw_from_d3d7(iface);
4135 D3DDEVICEDESC7 device_desc7;
4136 D3DDEVICEDESC device_desc1;
4137 HRESULT hr;
4139 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4141 EnterCriticalSection(&ddraw_cs);
4143 hr = IDirect3DImpl_GetCaps(ddraw->wineD3D, &device_desc1, &device_desc7);
4144 if (hr != D3D_OK)
4146 LeaveCriticalSection(&ddraw_cs);
4147 return hr;
4149 callback(interface_name_tnl, device_name_tnl, &device_desc7, context);
4151 device_desc7.deviceGUID = IID_IDirect3DHALDevice;
4152 callback(interface_name_hal, device_name_hal, &device_desc7, context);
4154 device_desc7.deviceGUID = IID_IDirect3DRGBDevice;
4155 callback(interface_name_rgb, device_name_rgb, &device_desc7, context);
4157 TRACE("End of enumeration.\n");
4159 LeaveCriticalSection(&ddraw_cs);
4161 return D3D_OK;
4164 /*****************************************************************************
4165 * IDirect3D3::EnumDevices
4167 * Enumerates all supported Direct3DDevice interfaces. This is the
4168 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
4170 * Version 1, 2 and 3
4172 * Params:
4173 * callback: Application-provided routine to call for each enumerated device
4174 * Context: Pointer to pass to the callback
4176 * Returns:
4177 * D3D_OK on success,
4178 * The result of IDirect3DImpl_GetCaps if it failed
4180 *****************************************************************************/
4181 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4183 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
4185 IDirectDrawImpl *ddraw = ddraw_from_d3d3(iface);
4186 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
4187 D3DDEVICEDESC7 device_desc7;
4188 HRESULT hr;
4190 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
4191 * name string. Let's put the string in a sufficiently sized array in
4192 * writable memory. */
4193 char device_name[50];
4194 strcpy(device_name,"Direct3D HEL");
4196 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4198 EnterCriticalSection(&ddraw_cs);
4200 hr = IDirect3DImpl_GetCaps(ddraw->wineD3D, &device_desc1, &device_desc7);
4201 if (hr != D3D_OK)
4203 LeaveCriticalSection(&ddraw_cs);
4204 return hr;
4207 /* Do I have to enumerate the reference id? Note from old d3d7:
4208 * "It seems that enumerating the reference IID on Direct3D 1 games
4209 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
4211 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
4212 * EnumReference which enables / disables enumerating the reference
4213 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
4214 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
4215 * demo directory suggest this.
4217 * Some games(GTA 2) seem to use the second enumerated device, so I have
4218 * to enumerate at least 2 devices. So enumerate the reference device to
4219 * have 2 devices.
4221 * Other games (Rollcage) tell emulation and hal device apart by certain
4222 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
4223 * limitation flag), and it refuses all devices that have the perspective
4224 * flag set. This way it refuses the emulation device, and HAL devices
4225 * never have POW2 unset in d3d7 on windows. */
4226 if (ddraw->d3dversion != 1)
4228 static CHAR reference_description[] = "RGB Direct3D emulation";
4230 TRACE("Enumerating WineD3D D3DDevice interface.\n");
4231 hal_desc = device_desc1;
4232 hel_desc = device_desc1;
4233 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
4234 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4235 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4236 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4237 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4238 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
4239 device_name, &hal_desc, &hel_desc, context);
4240 if (hr != D3DENUMRET_OK)
4242 TRACE("Application cancelled the enumeration.\n");
4243 LeaveCriticalSection(&ddraw_cs);
4244 return D3D_OK;
4248 strcpy(device_name,"Direct3D HAL");
4250 TRACE("Enumerating HAL Direct3D device.\n");
4251 hal_desc = device_desc1;
4252 hel_desc = device_desc1;
4253 /* The hal device does not have the pow2 flag set in hel, but in hal. */
4254 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4255 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4256 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
4257 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
4258 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
4259 device_name, &hal_desc, &hel_desc, context);
4260 if (hr != D3DENUMRET_OK)
4262 TRACE("Application cancelled the enumeration.\n");
4263 LeaveCriticalSection(&ddraw_cs);
4264 return D3D_OK;
4267 TRACE("End of enumeration.\n");
4269 LeaveCriticalSection(&ddraw_cs);
4270 return D3D_OK;
4273 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4275 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4277 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4279 return d3d3_EnumDevices((IDirect3D3 *)&This->IDirect3D3_vtbl, callback, context);
4282 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4284 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4286 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4288 return d3d3_EnumDevices((IDirect3D3 *)&This->IDirect3D3_vtbl, callback, context);
4291 /*****************************************************************************
4292 * IDirect3D3::CreateLight
4294 * Creates an IDirect3DLight interface. This interface is used in
4295 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4296 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4297 * uses the IDirect3DDevice7 interface with D3D7 lights.
4299 * Version 1, 2 and 3
4301 * Params:
4302 * light: Address to store the new interface pointer
4303 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4304 * Must be NULL
4306 * Returns:
4307 * D3D_OK on success
4308 * DDERR_OUTOFMEMORY if memory allocation failed
4309 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4311 *****************************************************************************/
4312 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4314 IDirect3DLightImpl *object;
4316 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4318 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4320 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4321 if (!object)
4323 ERR("Failed to allocate light memory.\n");
4324 return DDERR_OUTOFMEMORY;
4327 d3d_light_init(object, ddraw_from_d3d3(iface));
4329 TRACE("Created light %p.\n", object);
4330 *light = (IDirect3DLight *)object;
4332 return D3D_OK;
4335 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4337 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4339 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4341 return d3d3_CreateLight((IDirect3D3 *)&This->IDirect3D3_vtbl, light, outer_unknown);
4344 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4346 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4348 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4350 return d3d3_CreateLight((IDirect3D3 *)&This->IDirect3D3_vtbl, light, outer_unknown);
4353 /*****************************************************************************
4354 * IDirect3D3::CreateMaterial
4356 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4357 * and older versions. The IDirect3DMaterial implementation wraps its
4358 * functionality to IDirect3DDevice7::SetMaterial and friends.
4360 * Version 1, 2 and 3
4362 * Params:
4363 * material: Address to store the new interface's pointer to
4364 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4365 * Must be NULL
4367 * Returns:
4368 * D3D_OK on success
4369 * DDERR_OUTOFMEMORY if memory allocation failed
4370 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4372 *****************************************************************************/
4373 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material, IUnknown *outer_unknown)
4375 IDirect3DMaterialImpl *object;
4377 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4379 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4381 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4382 if (!object)
4384 ERR("Failed to allocate material memory.\n");
4385 return DDERR_OUTOFMEMORY;
4388 d3d_material_init(object, ddraw_from_d3d3(iface));
4390 TRACE("Created material %p.\n", object);
4391 *material = (IDirect3DMaterial3 *)object;
4393 return D3D_OK;
4396 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material, IUnknown *outer_unknown)
4398 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4399 IDirect3DMaterial3 *material3;
4400 HRESULT hr;
4402 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4404 hr = d3d3_CreateMaterial((IDirect3D3 *)&This->IDirect3D3_vtbl, &material3, outer_unknown);
4405 *material = material3 ? (IDirect3DMaterial2 *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial2_vtbl : NULL;
4407 TRACE("Returning material %p.\n", *material);
4409 return hr;
4412 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material, IUnknown *outer_unknown)
4414 IDirect3DMaterial3 *material3;
4415 HRESULT hr;
4417 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4419 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4421 hr = d3d3_CreateMaterial((IDirect3D3 *)&This->IDirect3D3_vtbl, &material3, outer_unknown);
4422 *material = material3 ? (IDirect3DMaterial *)&((IDirect3DMaterialImpl *)material3)->IDirect3DMaterial_vtbl : NULL;
4424 TRACE("Returning material %p.\n", *material);
4426 return hr;
4429 /*****************************************************************************
4430 * IDirect3D3::CreateViewport
4432 * Creates an IDirect3DViewport interface. This interface is used
4433 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4434 * it has been replaced by a viewport structure and
4435 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4436 * uses the IDirect3DDevice7 methods for its functionality
4438 * Params:
4439 * Viewport: Address to store the new interface pointer
4440 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4441 * Must be NULL
4443 * Returns:
4444 * D3D_OK on success
4445 * DDERR_OUTOFMEMORY if memory allocation failed
4446 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4448 *****************************************************************************/
4449 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport, IUnknown *outer_unknown)
4451 IDirect3DViewportImpl *object;
4453 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4455 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4457 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4458 if (!object)
4460 ERR("Failed to allocate viewport memory.\n");
4461 return DDERR_OUTOFMEMORY;
4464 d3d_viewport_init(object, ddraw_from_d3d3(iface));
4466 TRACE("Created viewport %p.\n", object);
4467 *viewport = (IDirect3DViewport3 *)object;
4469 return D3D_OK;
4472 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4474 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4476 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4478 return d3d3_CreateViewport((IDirect3D3 *)&This->IDirect3D3_vtbl,
4479 (IDirect3DViewport3 **)viewport, outer_unknown);
4482 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4484 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4486 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4488 return d3d3_CreateViewport((IDirect3D3 *)&This->IDirect3D3_vtbl,
4489 (IDirect3DViewport3 **)viewport, outer_unknown);
4492 /*****************************************************************************
4493 * IDirect3D3::FindDevice
4495 * This method finds a device with the requested properties and returns a
4496 * device description
4498 * Verion 1, 2 and 3
4499 * Params:
4500 * fds: Describes the requested device characteristics
4501 * fdr: Returns the device description
4503 * Returns:
4504 * D3D_OK on success
4505 * DDERR_INVALIDPARAMS if no device was found
4507 *****************************************************************************/
4508 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4510 IDirectDrawImpl *ddraw = ddraw_from_d3d3(iface);
4511 D3DDEVICEDESC7 desc7;
4512 D3DDEVICEDESC desc1;
4513 HRESULT hr;
4515 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4517 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4519 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4520 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4521 return DDERR_INVALIDPARAMS;
4523 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4524 && fds->dcmColorModel != D3DCOLOR_RGB)
4526 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4527 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4530 if (fds->dwFlags & D3DFDS_GUID)
4532 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4533 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4534 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4535 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4537 WARN("No match for this GUID.\n");
4538 return DDERR_NOTFOUND;
4542 /* Get the caps */
4543 hr = IDirect3DImpl_GetCaps(ddraw->wineD3D, &desc1, &desc7);
4544 if (hr != D3D_OK) return hr;
4546 /* Now return our own GUID */
4547 fdr->guid = IID_D3DDEVICE_WineD3D;
4548 fdr->ddHwDesc = desc1;
4549 fdr->ddSwDesc = desc1;
4551 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4553 return D3D_OK;
4556 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4558 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4560 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4562 return d3d3_FindDevice((IDirect3D3 *)&This->IDirect3D3_vtbl, fds, fdr);
4565 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4567 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4569 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4571 return d3d3_FindDevice((IDirect3D3 *)&This->IDirect3D3_vtbl, fds, fdr);
4574 /*****************************************************************************
4575 * IDirect3D7::CreateDevice
4577 * Creates an IDirect3DDevice7 interface.
4579 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4580 * DirectDraw surfaces and are created with
4581 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4582 * create the device object and QueryInterfaces for IDirect3DDevice
4584 * Params:
4585 * refiid: IID of the device to create
4586 * Surface: Initial rendertarget
4587 * Device: Address to return the interface pointer
4589 * Returns:
4590 * D3D_OK on success
4591 * DDERR_OUTOFMEMORY if memory allocation failed
4592 * DDERR_INVALIDPARAMS if a device exists already
4594 *****************************************************************************/
4595 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4596 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4598 IDirectDrawSurfaceImpl *target = (IDirectDrawSurfaceImpl *)surface;
4599 IDirectDrawImpl *ddraw = ddraw_from_d3d7(iface);
4600 IDirect3DDeviceImpl *object;
4601 HRESULT hr;
4603 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4605 EnterCriticalSection(&ddraw_cs);
4606 *device = NULL;
4608 /* Fail device creation if non-opengl surfaces are used. */
4609 if (ddraw->ImplType != SURFACE_OPENGL)
4611 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4612 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4614 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4615 * is caught in CreateSurface or QueryInterface. */
4616 LeaveCriticalSection(&ddraw_cs);
4617 return DDERR_NO3D;
4620 if (ddraw->d3ddevice)
4622 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4623 LeaveCriticalSection(&ddraw_cs);
4624 return DDERR_INVALIDPARAMS;
4627 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4628 if (!object)
4630 ERR("Failed to allocate device memory.\n");
4631 LeaveCriticalSection(&ddraw_cs);
4632 return DDERR_OUTOFMEMORY;
4635 hr = d3d_device_init(object, ddraw, target);
4636 if (FAILED(hr))
4638 WARN("Failed to initialize device, hr %#x.\n", hr);
4639 HeapFree(GetProcessHeap(), 0, object);
4640 LeaveCriticalSection(&ddraw_cs);
4641 return hr;
4644 TRACE("Created device %p.\n", object);
4645 *device = (IDirect3DDevice7 *)object;
4647 LeaveCriticalSection(&ddraw_cs);
4648 return D3D_OK;
4651 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4652 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4654 HRESULT hr;
4656 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4657 iface, debugstr_guid(riid), surface, device, outer_unknown);
4659 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4661 hr = d3d7_CreateDevice((IDirect3D7 *)&ddraw_from_d3d3(iface)->IDirect3D7_vtbl, riid,
4662 (IDirectDrawSurface7 *)surface, (IDirect3DDevice7 **)device);
4663 if (*device) *device = (IDirect3DDevice3 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice3_vtbl;
4665 return hr;
4668 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4669 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4671 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4672 HRESULT hr;
4674 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4675 iface, debugstr_guid(riid), surface, device);
4677 hr = d3d7_CreateDevice((IDirect3D7 *)&This->IDirect3D7_vtbl, riid,
4678 surface ? (IDirectDrawSurface7 *)surface_from_surface3((IDirectDrawSurface3 *)surface) : NULL,
4679 (IDirect3DDevice7 **)device);
4680 if (*device) *device = (IDirect3DDevice2 *)&((IDirect3DDeviceImpl *)*device)->IDirect3DDevice2_vtbl;
4682 return hr;
4685 /*****************************************************************************
4686 * IDirect3D7::CreateVertexBuffer
4688 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4689 * interface.
4691 * Version 3 and 7
4693 * Params:
4694 * desc: Requested Vertex buffer properties
4695 * vertex_buffer: Address to return the interface pointer at
4696 * flags: Some flags, should be 0
4698 * Returns
4699 * D3D_OK on success
4700 * DDERR_OUTOFMEMORY if memory allocation failed
4701 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4702 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4704 *****************************************************************************/
4705 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4706 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4708 IDirect3DVertexBufferImpl *object;
4709 HRESULT hr;
4711 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4712 iface, desc, vertex_buffer, flags);
4714 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4716 TRACE("Vertex buffer description:\n");
4717 TRACE(" dwSize %u\n", desc->dwSize);
4718 TRACE(" dwCaps %#x\n", desc->dwCaps);
4719 TRACE(" FVF %#x\n", desc->dwFVF);
4720 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
4722 /* Now create the vertex buffer */
4723 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4724 if (!object)
4726 ERR("Failed to allocate vertex buffer memory.\n");
4727 return DDERR_OUTOFMEMORY;
4730 hr = d3d_vertex_buffer_init(object, ddraw_from_d3d7(iface), desc);
4731 if (FAILED(hr))
4733 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
4734 HeapFree(GetProcessHeap(), 0, object);
4735 return hr;
4738 TRACE("Created vertex buffer %p.\n", object);
4739 *vertex_buffer = (IDirect3DVertexBuffer7 *)object;
4741 return D3D_OK;
4744 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4745 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4747 IDirectDrawImpl *This = ddraw_from_d3d3(iface);
4748 HRESULT hr;
4750 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4751 iface, desc, vertex_buffer, flags, outer_unknown);
4753 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4755 hr = d3d7_CreateVertexBuffer((IDirect3D7 *)&This->IDirect3D7_vtbl,
4756 desc, (IDirect3DVertexBuffer7 **)vertex_buffer, flags);
4757 if (*vertex_buffer)
4758 *vertex_buffer = (IDirect3DVertexBuffer *)&((IDirect3DVertexBufferImpl *)*vertex_buffer)->IDirect3DVertexBuffer_vtbl;
4760 return hr;
4763 /*****************************************************************************
4764 * IDirect3D7::EnumZBufferFormats
4766 * Enumerates all supported Z buffer pixel formats
4768 * Version 3 and 7
4770 * Params:
4771 * device_iid:
4772 * callback: callback to call for each pixel format
4773 * context: Pointer to pass back to the callback
4775 * Returns:
4776 * D3D_OK on success
4777 * DDERR_INVALIDPARAMS if callback is NULL
4778 * For details, see IWineD3DDevice::EnumZBufferFormats
4780 *****************************************************************************/
4781 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4782 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4784 IDirectDrawImpl *ddraw = ddraw_from_d3d7(iface);
4785 WINED3DDISPLAYMODE d3ddm;
4786 WINED3DDEVTYPE type;
4787 unsigned int i;
4788 HRESULT hr;
4790 /* Order matters. Specifically, BattleZone II (full version) expects the
4791 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4792 static const enum wined3d_format_id formats[] =
4794 WINED3DFMT_S1_UINT_D15_UNORM,
4795 WINED3DFMT_D16_UNORM,
4796 WINED3DFMT_X8D24_UNORM,
4797 WINED3DFMT_S4X4_UINT_D24_UNORM,
4798 WINED3DFMT_D24_UNORM_S8_UINT,
4799 WINED3DFMT_D32_UNORM,
4802 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4803 iface, debugstr_guid(device_iid), callback, context);
4805 if (!callback) return DDERR_INVALIDPARAMS;
4807 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4808 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4809 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4811 TRACE("Asked for HAL device.\n");
4812 type = WINED3DDEVTYPE_HAL;
4814 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4815 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4817 TRACE("Asked for SW device.\n");
4818 type = WINED3DDEVTYPE_SW;
4820 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4822 TRACE("Asked for REF device.\n");
4823 type = WINED3DDEVTYPE_REF;
4825 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4827 TRACE("Asked for NULLREF device.\n");
4828 type = WINED3DDEVTYPE_NULLREF;
4830 else
4832 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4833 type = WINED3DDEVTYPE_HAL;
4836 EnterCriticalSection(&ddraw_cs);
4837 /* We need an adapter format from somewhere to please wined3d and WGL.
4838 * Use the current display mode. So far all cards offer the same depth
4839 * stencil format for all modes, but if some do not and applications do
4840 * not like that we'll have to find some workaround, like iterating over
4841 * all imaginable formats and collecting all the depth stencil formats we
4842 * can get. */
4843 hr = IWineD3DDevice_GetDisplayMode(ddraw->wineD3DDevice, 0, &d3ddm);
4845 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4847 hr = IWineD3D_CheckDeviceFormat(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, type, d3ddm.Format,
4848 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4849 if (SUCCEEDED(hr))
4851 DDPIXELFORMAT pformat;
4853 memset(&pformat, 0, sizeof(pformat));
4854 pformat.dwSize = sizeof(pformat);
4855 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4857 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4858 hr = callback(&pformat, context);
4859 if (hr != DDENUMRET_OK)
4861 TRACE("Format enumeration cancelled by application.\n");
4862 LeaveCriticalSection(&ddraw_cs);
4863 return D3D_OK;
4867 TRACE("End of enumeration.\n");
4869 LeaveCriticalSection(&ddraw_cs);
4870 return D3D_OK;
4873 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4874 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4876 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4877 iface, debugstr_guid(device_iid), callback, context);
4879 return d3d7_EnumZBufferFormats((IDirect3D7 *)&ddraw_from_d3d3(iface)->IDirect3D7_vtbl,
4880 device_iid, callback, context);
4883 /*****************************************************************************
4884 * IDirect3D7::EvictManagedTextures
4886 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4887 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4889 * Version 3 and 7
4891 * Returns:
4892 * D3D_OK, because it's a stub
4894 *****************************************************************************/
4895 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4897 FIXME("iface %p stub!\n", iface);
4899 /* TODO: Just enumerate resources using IWineD3DDevice_EnumResources(),
4900 * then unload surfaces / textures. */
4902 return D3D_OK;
4905 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4907 TRACE("iface %p.\n", iface);
4909 return d3d7_EvictManagedTextures((IDirect3D7 *)&ddraw_from_d3d3(iface)->IDirect3D7_vtbl);
4912 /*****************************************************************************
4913 * IDirect3DImpl_GetCaps
4915 * This function retrieves the device caps from wined3d
4916 * and converts it into a D3D7 and D3D - D3D3 structure
4917 * This is a helper function called from various places in ddraw
4919 * Params:
4920 * wined3d: The interface to get the caps from
4921 * desc1: Old D3D <3 structure to fill (needed)
4922 * desc7: D3D7 device desc structure to fill (needed)
4924 * Returns
4925 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4927 *****************************************************************************/
4928 HRESULT IDirect3DImpl_GetCaps(IWineD3D *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4930 WINED3DCAPS wined3d_caps;
4931 HRESULT hr;
4933 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4935 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4937 EnterCriticalSection(&ddraw_cs);
4938 hr = IWineD3D_GetDeviceCaps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
4939 LeaveCriticalSection(&ddraw_cs);
4940 if (FAILED(hr))
4942 WARN("Failed to get device caps, hr %#x.\n", hr);
4943 return hr;
4946 /* Copy the results into the d3d7 and d3d3 structures */
4947 desc7->dwDevCaps = wined3d_caps.DevCaps;
4948 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4949 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4950 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4951 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4952 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4953 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4954 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4955 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4956 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4957 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4959 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4960 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4962 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4963 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4964 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4965 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4967 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4968 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4969 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4970 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4972 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4973 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4975 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4976 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4978 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4979 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4981 /* Remove all non-d3d7 caps */
4982 desc7->dwDevCaps &= (
4983 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4984 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4985 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4986 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4987 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4988 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4989 D3DDEVCAPS_HWRASTERIZATION);
4991 desc7->dwStencilCaps &= (
4992 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4993 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4994 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4996 /* FVF caps ?*/
4998 desc7->dwTextureOpCaps &= (
4999 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
5000 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
5001 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
5002 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
5003 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
5004 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
5005 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
5006 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
5008 desc7->dwVertexProcessingCaps &= (
5009 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
5010 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
5012 desc7->dpcLineCaps.dwMiscCaps &= (
5013 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
5014 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
5015 D3DPMISCCAPS_CULLCCW);
5017 desc7->dpcLineCaps.dwRasterCaps &= (
5018 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
5019 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
5020 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
5021 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
5022 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
5023 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
5024 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
5025 D3DPRASTERCAPS_ZFOG);
5027 desc7->dpcLineCaps.dwZCmpCaps &= (
5028 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5029 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5030 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5032 desc7->dpcLineCaps.dwSrcBlendCaps &= (
5033 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5034 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5035 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5036 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5037 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5039 desc7->dpcLineCaps.dwDestBlendCaps &= (
5040 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
5041 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
5042 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
5043 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
5044 D3DPBLENDCAPS_BOTHINVSRCALPHA);
5046 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
5047 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
5048 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
5049 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
5051 desc7->dpcLineCaps.dwShadeCaps &= (
5052 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
5053 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
5054 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
5055 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
5056 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
5057 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
5058 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
5060 desc7->dpcLineCaps.dwTextureCaps &= (
5061 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
5062 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
5063 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
5064 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
5066 desc7->dpcLineCaps.dwTextureFilterCaps &= (
5067 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
5068 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
5069 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
5070 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
5071 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
5072 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
5074 desc7->dpcLineCaps.dwTextureBlendCaps &= (
5075 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
5076 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
5077 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
5079 desc7->dpcLineCaps.dwTextureAddressCaps &= (
5080 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
5081 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
5083 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
5085 /* DirectX7 always has the np2 flag set, no matter what the card
5086 * supports. Some old games (Rollcage) check the caps incorrectly.
5087 * If wined3d supports nonpow2 textures it also has np2 conditional
5088 * support. */
5089 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
5092 /* Fill the missing members, and do some fixup */
5093 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
5094 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
5095 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
5096 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
5097 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
5098 desc7->dpcLineCaps.dwStippleWidth = 32;
5099 desc7->dpcLineCaps.dwStippleHeight = 32;
5100 /* Use the same for the TriCaps */
5101 desc7->dpcTriCaps = desc7->dpcLineCaps;
5103 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
5104 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
5105 desc7->dwMinTextureWidth = 1;
5106 desc7->dwMinTextureHeight = 1;
5108 /* Convert DWORDs safely to WORDs */
5109 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
5110 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
5111 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
5112 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
5114 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
5115 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
5116 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
5117 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
5119 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
5121 desc7->dwReserved1 = 0;
5122 desc7->dwReserved2 = 0;
5123 desc7->dwReserved3 = 0;
5124 desc7->dwReserved4 = 0;
5126 /* Fill the old structure */
5127 memset(desc1, 0, sizeof(*desc1));
5128 desc1->dwSize = sizeof(D3DDEVICEDESC);
5129 desc1->dwFlags = D3DDD_COLORMODEL
5130 | D3DDD_DEVCAPS
5131 | D3DDD_TRANSFORMCAPS
5132 | D3DDD_BCLIPPING
5133 | D3DDD_LIGHTINGCAPS
5134 | D3DDD_LINECAPS
5135 | D3DDD_TRICAPS
5136 | D3DDD_DEVICERENDERBITDEPTH
5137 | D3DDD_DEVICEZBUFFERBITDEPTH
5138 | D3DDD_MAXBUFFERSIZE
5139 | D3DDD_MAXVERTEXCOUNT;
5141 desc1->dcmColorModel = D3DCOLOR_RGB;
5142 desc1->dwDevCaps = desc7->dwDevCaps;
5143 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
5144 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
5145 desc1->bClipping = TRUE;
5146 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
5147 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
5148 | D3DLIGHTCAPS_PARALLELPOINT
5149 | D3DLIGHTCAPS_POINT
5150 | D3DLIGHTCAPS_SPOT;
5152 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
5153 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
5155 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
5156 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
5157 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
5158 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
5159 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
5160 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
5161 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
5162 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
5163 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
5164 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
5165 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
5166 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
5167 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
5169 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
5170 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
5171 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
5172 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
5173 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
5174 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
5175 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
5176 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
5177 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
5178 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
5179 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
5180 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
5181 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
5183 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
5184 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
5185 desc1->dwMaxBufferSize = 0;
5186 desc1->dwMaxVertexCount = 65536;
5187 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
5188 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
5189 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
5190 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
5191 desc1->dwMinStippleWidth = 1;
5192 desc1->dwMinStippleHeight = 1;
5193 desc1->dwMaxStippleWidth = 32;
5194 desc1->dwMaxStippleHeight = 32;
5195 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
5196 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
5197 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
5198 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
5199 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
5200 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
5201 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
5202 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
5203 desc1->dwStencilCaps = desc7->dwStencilCaps;
5204 desc1->dwFVFCaps = desc7->dwFVFCaps;
5205 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
5206 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
5207 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
5209 return DD_OK;
5212 /*****************************************************************************
5213 * IDirectDraw7 VTable
5214 *****************************************************************************/
5215 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
5217 /* IUnknown */
5218 ddraw7_QueryInterface,
5219 ddraw7_AddRef,
5220 ddraw7_Release,
5221 /* IDirectDraw */
5222 ddraw7_Compact,
5223 ddraw7_CreateClipper,
5224 ddraw7_CreatePalette,
5225 ddraw7_CreateSurface,
5226 ddraw7_DuplicateSurface,
5227 ddraw7_EnumDisplayModes,
5228 ddraw7_EnumSurfaces,
5229 ddraw7_FlipToGDISurface,
5230 ddraw7_GetCaps,
5231 ddraw7_GetDisplayMode,
5232 ddraw7_GetFourCCCodes,
5233 ddraw7_GetGDISurface,
5234 ddraw7_GetMonitorFrequency,
5235 ddraw7_GetScanLine,
5236 ddraw7_GetVerticalBlankStatus,
5237 ddraw7_Initialize,
5238 ddraw7_RestoreDisplayMode,
5239 ddraw7_SetCooperativeLevel,
5240 ddraw7_SetDisplayMode,
5241 ddraw7_WaitForVerticalBlank,
5242 /* IDirectDraw2 */
5243 ddraw7_GetAvailableVidMem,
5244 /* IDirectDraw3 */
5245 ddraw7_GetSurfaceFromDC,
5246 /* IDirectDraw4 */
5247 ddraw7_RestoreAllSurfaces,
5248 ddraw7_TestCooperativeLevel,
5249 ddraw7_GetDeviceIdentifier,
5250 /* IDirectDraw7 */
5251 ddraw7_StartModeTest,
5252 ddraw7_EvaluateMode
5255 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5257 /* IUnknown */
5258 ddraw4_QueryInterface,
5259 ddraw4_AddRef,
5260 ddraw4_Release,
5261 /* IDirectDraw */
5262 ddraw4_Compact,
5263 ddraw4_CreateClipper,
5264 ddraw4_CreatePalette,
5265 ddraw4_CreateSurface,
5266 ddraw4_DuplicateSurface,
5267 ddraw4_EnumDisplayModes,
5268 ddraw4_EnumSurfaces,
5269 ddraw4_FlipToGDISurface,
5270 ddraw4_GetCaps,
5271 ddraw4_GetDisplayMode,
5272 ddraw4_GetFourCCCodes,
5273 ddraw4_GetGDISurface,
5274 ddraw4_GetMonitorFrequency,
5275 ddraw4_GetScanLine,
5276 ddraw4_GetVerticalBlankStatus,
5277 ddraw4_Initialize,
5278 ddraw4_RestoreDisplayMode,
5279 ddraw4_SetCooperativeLevel,
5280 ddraw4_SetDisplayMode,
5281 ddraw4_WaitForVerticalBlank,
5282 /* IDirectDraw2 */
5283 ddraw4_GetAvailableVidMem,
5284 /* IDirectDraw3 */
5285 ddraw4_GetSurfaceFromDC,
5286 /* IDirectDraw4 */
5287 ddraw4_RestoreAllSurfaces,
5288 ddraw4_TestCooperativeLevel,
5289 ddraw4_GetDeviceIdentifier,
5292 static const struct IDirectDraw3Vtbl ddraw3_vtbl =
5294 /* IUnknown */
5295 ddraw3_QueryInterface,
5296 ddraw3_AddRef,
5297 ddraw3_Release,
5298 /* IDirectDraw */
5299 ddraw3_Compact,
5300 ddraw3_CreateClipper,
5301 ddraw3_CreatePalette,
5302 ddraw3_CreateSurface,
5303 ddraw3_DuplicateSurface,
5304 ddraw3_EnumDisplayModes,
5305 ddraw3_EnumSurfaces,
5306 ddraw3_FlipToGDISurface,
5307 ddraw3_GetCaps,
5308 ddraw3_GetDisplayMode,
5309 ddraw3_GetFourCCCodes,
5310 ddraw3_GetGDISurface,
5311 ddraw3_GetMonitorFrequency,
5312 ddraw3_GetScanLine,
5313 ddraw3_GetVerticalBlankStatus,
5314 ddraw3_Initialize,
5315 ddraw3_RestoreDisplayMode,
5316 ddraw3_SetCooperativeLevel,
5317 ddraw3_SetDisplayMode,
5318 ddraw3_WaitForVerticalBlank,
5319 /* IDirectDraw2 */
5320 ddraw3_GetAvailableVidMem,
5321 /* IDirectDraw3 */
5322 ddraw3_GetSurfaceFromDC,
5325 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5327 /* IUnknown */
5328 ddraw2_QueryInterface,
5329 ddraw2_AddRef,
5330 ddraw2_Release,
5331 /* IDirectDraw */
5332 ddraw2_Compact,
5333 ddraw2_CreateClipper,
5334 ddraw2_CreatePalette,
5335 ddraw2_CreateSurface,
5336 ddraw2_DuplicateSurface,
5337 ddraw2_EnumDisplayModes,
5338 ddraw2_EnumSurfaces,
5339 ddraw2_FlipToGDISurface,
5340 ddraw2_GetCaps,
5341 ddraw2_GetDisplayMode,
5342 ddraw2_GetFourCCCodes,
5343 ddraw2_GetGDISurface,
5344 ddraw2_GetMonitorFrequency,
5345 ddraw2_GetScanLine,
5346 ddraw2_GetVerticalBlankStatus,
5347 ddraw2_Initialize,
5348 ddraw2_RestoreDisplayMode,
5349 ddraw2_SetCooperativeLevel,
5350 ddraw2_SetDisplayMode,
5351 ddraw2_WaitForVerticalBlank,
5352 /* IDirectDraw2 */
5353 ddraw2_GetAvailableVidMem,
5356 static const struct IDirectDrawVtbl ddraw1_vtbl =
5358 /* IUnknown */
5359 ddraw1_QueryInterface,
5360 ddraw1_AddRef,
5361 ddraw1_Release,
5362 /* IDirectDraw */
5363 ddraw1_Compact,
5364 ddraw1_CreateClipper,
5365 ddraw1_CreatePalette,
5366 ddraw1_CreateSurface,
5367 ddraw1_DuplicateSurface,
5368 ddraw1_EnumDisplayModes,
5369 ddraw1_EnumSurfaces,
5370 ddraw1_FlipToGDISurface,
5371 ddraw1_GetCaps,
5372 ddraw1_GetDisplayMode,
5373 ddraw1_GetFourCCCodes,
5374 ddraw1_GetGDISurface,
5375 ddraw1_GetMonitorFrequency,
5376 ddraw1_GetScanLine,
5377 ddraw1_GetVerticalBlankStatus,
5378 ddraw1_Initialize,
5379 ddraw1_RestoreDisplayMode,
5380 ddraw1_SetCooperativeLevel,
5381 ddraw1_SetDisplayMode,
5382 ddraw1_WaitForVerticalBlank,
5385 static const struct IDirect3D7Vtbl d3d7_vtbl =
5387 /* IUnknown methods */
5388 d3d7_QueryInterface,
5389 d3d7_AddRef,
5390 d3d7_Release,
5391 /* IDirect3D7 methods */
5392 d3d7_EnumDevices,
5393 d3d7_CreateDevice,
5394 d3d7_CreateVertexBuffer,
5395 d3d7_EnumZBufferFormats,
5396 d3d7_EvictManagedTextures
5399 static const struct IDirect3D3Vtbl d3d3_vtbl =
5401 /* IUnknown methods */
5402 d3d3_QueryInterface,
5403 d3d3_AddRef,
5404 d3d3_Release,
5405 /* IDirect3D3 methods */
5406 d3d3_EnumDevices,
5407 d3d3_CreateLight,
5408 d3d3_CreateMaterial,
5409 d3d3_CreateViewport,
5410 d3d3_FindDevice,
5411 d3d3_CreateDevice,
5412 d3d3_CreateVertexBuffer,
5413 d3d3_EnumZBufferFormats,
5414 d3d3_EvictManagedTextures
5417 static const struct IDirect3D2Vtbl d3d2_vtbl =
5419 /* IUnknown methods */
5420 d3d2_QueryInterface,
5421 d3d2_AddRef,
5422 d3d2_Release,
5423 /* IDirect3D2 methods */
5424 d3d2_EnumDevices,
5425 d3d2_CreateLight,
5426 d3d2_CreateMaterial,
5427 d3d2_CreateViewport,
5428 d3d2_FindDevice,
5429 d3d2_CreateDevice
5432 static const struct IDirect3DVtbl d3d1_vtbl =
5434 /* IUnknown methods */
5435 d3d1_QueryInterface,
5436 d3d1_AddRef,
5437 d3d1_Release,
5438 /* IDirect3D methods */
5439 d3d1_Initialize,
5440 d3d1_EnumDevices,
5441 d3d1_CreateLight,
5442 d3d1_CreateMaterial,
5443 d3d1_CreateViewport,
5444 d3d1_FindDevice
5447 /*****************************************************************************
5448 * ddraw_find_decl
5450 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5451 * if none was found.
5453 * This function is in ddraw.c and the DDraw object space because D3D7
5454 * vertex buffers are created using the IDirect3D interface to the ddraw
5455 * object, so they can be valid across D3D devices(theoretically. The ddraw
5456 * object also owns the wined3d device
5458 * Parameters:
5459 * This: Device
5460 * fvf: Fvf to find the decl for
5462 * Returns:
5463 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
5464 * fvf otherwise.
5466 *****************************************************************************/
5467 IWineD3DVertexDeclaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5469 HRESULT hr;
5470 IWineD3DVertexDeclaration* pDecl = NULL;
5471 int p, low, high; /* deliberately signed */
5472 struct FvfToDecl *convertedDecls = This->decls;
5474 TRACE("Searching for declaration for fvf %08x... ", fvf);
5476 low = 0;
5477 high = This->numConvertedDecls - 1;
5478 while(low <= high) {
5479 p = (low + high) >> 1;
5480 TRACE("%d ", p);
5481 if(convertedDecls[p].fvf == fvf) {
5482 TRACE("found %p\n", convertedDecls[p].decl);
5483 return convertedDecls[p].decl;
5484 } else if(convertedDecls[p].fvf < fvf) {
5485 low = p + 1;
5486 } else {
5487 high = p - 1;
5490 TRACE("not found. Creating and inserting at position %d.\n", low);
5492 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
5493 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5494 if (hr != S_OK) return NULL;
5496 if(This->declArraySize == This->numConvertedDecls) {
5497 int grow = max(This->declArraySize / 2, 8);
5498 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5499 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5500 if(!convertedDecls) {
5501 /* This will destroy it */
5502 IWineD3DVertexDeclaration_Release(pDecl);
5503 return NULL;
5505 This->decls = convertedDecls;
5506 This->declArraySize += grow;
5509 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5510 convertedDecls[low].decl = pDecl;
5511 convertedDecls[low].fvf = fvf;
5512 This->numConvertedDecls++;
5514 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5515 return pDecl;
5518 /* IWineD3DDeviceParent IUnknown methods */
5520 static inline struct IDirectDrawImpl *ddraw_from_device_parent(IWineD3DDeviceParent *iface)
5522 return (struct IDirectDrawImpl *)((char*)iface - FIELD_OFFSET(struct IDirectDrawImpl, device_parent_vtbl));
5525 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
5527 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5528 return ddraw7_QueryInterface((IDirectDraw7 *)This, riid, object);
5531 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
5533 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5534 return ddraw7_AddRef((IDirectDraw7 *)This);
5537 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
5539 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5540 return ddraw7_Release((IDirectDraw7 *)This);
5543 /* IWineD3DDeviceParent methods */
5545 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
5547 TRACE("iface %p, device %p\n", iface, device);
5550 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
5551 IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5552 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
5554 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5555 IDirectDrawSurfaceImpl *surf = NULL;
5556 UINT i = 0;
5557 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
5559 TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
5560 "\tpool %#x, level %u, face %u, surface %p\n",
5561 iface, superior, width, height, format, usage, pool, level, face, surface);
5563 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5564 switch(face)
5566 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5567 TRACE("Asked for positive x\n");
5568 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5570 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5572 surf = This->tex_root; break;
5573 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5574 TRACE("Asked for negative x\n");
5575 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5576 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5577 TRACE("Asked for positive y\n");
5578 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5579 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5580 TRACE("Asked for negative y\n");
5581 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5582 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5583 TRACE("Asked for positive z\n");
5584 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5585 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5586 TRACE("Asked for negative z\n");
5587 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5588 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5591 if (!surf)
5593 IDirectDrawSurface7 *attached;
5594 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)This->tex_root, &searchcaps, &attached);
5595 surf = (IDirectDrawSurfaceImpl *)attached;
5596 IDirectDrawSurface7_Release(attached);
5598 if (!surf) ERR("root search surface not found\n");
5600 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5601 while (i < level)
5603 IDirectDrawSurface7 *attached;
5604 IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &searchcaps, &attached);
5605 if(!attached) ERR("Surface not found\n");
5606 surf = (IDirectDrawSurfaceImpl *)attached;
5607 IDirectDrawSurface7_Release(attached);
5608 ++i;
5611 /* Return the surface */
5612 *surface = surf->WineD3DSurface;
5613 IWineD3DSurface_AddRef(*surface);
5615 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5617 return D3D_OK;
5620 static HRESULT WINAPI findRenderTarget(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *ctx)
5622 IDirectDrawSurfaceImpl *s = (IDirectDrawSurfaceImpl *)surface;
5623 IDirectDrawSurfaceImpl **target = ctx;
5625 if (!s->isRenderTarget)
5627 *target = s;
5628 IDirectDrawSurface7_Release(surface);
5629 return DDENUMRET_CANCEL;
5632 /* Recurse into the surface tree */
5633 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
5635 IDirectDrawSurface7_Release(surface);
5636 if (*target) return DDENUMRET_CANCEL;
5638 return DDENUMRET_OK;
5641 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
5642 IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format,
5643 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5644 IWineD3DSurface **surface)
5646 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5647 IDirectDrawSurfaceImpl *d3d_surface = This->d3d_target;
5648 IDirectDrawSurfaceImpl *target = NULL;
5650 TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5651 "\tmultisample_quality %u, lockable %u, surface %p\n",
5652 iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
5654 if (d3d_surface->isRenderTarget)
5656 IDirectDrawSurface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)d3d_surface, &target, findRenderTarget);
5658 else
5660 target = d3d_surface;
5663 if (!target)
5665 target = This->d3d_target;
5666 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
5669 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
5671 *surface = target->WineD3DSurface;
5672 IWineD3DSurface_AddRef(*surface);
5673 target->isRenderTarget = TRUE;
5675 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, d3d_surface);
5677 return D3D_OK;
5680 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
5681 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5682 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
5684 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5685 IDirectDrawSurfaceImpl *ddraw_surface;
5686 DDSURFACEDESC2 ddsd;
5687 HRESULT hr;
5689 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5690 "\tmultisample_quality %u, discard %u, surface %p\n",
5691 iface, width, height, format, multisample_type, multisample_quality, discard, surface);
5693 *surface = NULL;
5695 /* Create a DirectDraw surface */
5696 memset(&ddsd, 0, sizeof(ddsd));
5697 ddsd.dwSize = sizeof(ddsd);
5698 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
5699 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
5700 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5701 ddsd.dwHeight = height;
5702 ddsd.dwWidth = width;
5703 if (format)
5705 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, format);
5707 else
5709 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
5712 This->depthstencil = TRUE;
5713 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *)This, &ddsd, (IDirectDrawSurface7 **)&ddraw_surface, NULL);
5714 This->depthstencil = FALSE;
5715 if(FAILED(hr))
5717 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
5718 return hr;
5721 *surface = ddraw_surface->WineD3DSurface;
5722 IWineD3DSurface_AddRef(*surface);
5723 IDirectDrawSurface7_Release((IDirectDrawSurface7 *)ddraw_surface);
5725 return D3D_OK;
5728 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
5729 IUnknown *superior, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5730 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
5732 TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
5733 iface, superior, width, height, depth, format, pool, usage, volume);
5735 ERR("Not implemented!\n");
5737 return E_NOTIMPL;
5740 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
5741 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
5743 struct IDirectDrawImpl *This = ddraw_from_device_parent(iface);
5744 IDirectDrawSurfaceImpl *iterator;
5745 IParentImpl *object;
5746 HRESULT hr;
5748 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
5750 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
5751 if (!object)
5753 FIXME("Allocation of memory failed\n");
5754 *swapchain = NULL;
5755 return DDERR_OUTOFVIDEOMEMORY;
5758 ddraw_parent_init(object);
5760 hr = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice, present_parameters,
5761 This->ImplType, object, swapchain);
5762 if (FAILED(hr))
5764 FIXME("(%p) CreateSwapChain failed, returning %#x\n", iface, hr);
5765 HeapFree(GetProcessHeap(), 0 , object);
5766 *swapchain = NULL;
5767 return hr;
5770 object->child = (IUnknown *)*swapchain;
5771 This->d3d_target->wineD3DSwapChain = *swapchain;
5772 iterator = This->d3d_target->complex_array[0];
5773 while (iterator)
5775 iterator->wineD3DSwapChain = *swapchain;
5776 iterator = iterator->complex_array[0];
5779 return hr;
5782 static const IWineD3DDeviceParentVtbl ddraw_wined3d_device_parent_vtbl =
5784 /* IUnknown methods */
5785 device_parent_QueryInterface,
5786 device_parent_AddRef,
5787 device_parent_Release,
5788 /* IWineD3DDeviceParent methods */
5789 device_parent_WineD3DDeviceCreated,
5790 device_parent_CreateSurface,
5791 device_parent_CreateRenderTarget,
5792 device_parent_CreateDepthStencilSurface,
5793 device_parent_CreateVolume,
5794 device_parent_CreateSwapChain,
5797 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5799 HRESULT hr;
5800 HDC hDC;
5802 ddraw->lpVtbl = &ddraw7_vtbl;
5803 ddraw->IDirectDraw_vtbl = &ddraw1_vtbl;
5804 ddraw->IDirectDraw2_vtbl = &ddraw2_vtbl;
5805 ddraw->IDirectDraw3_vtbl = &ddraw3_vtbl;
5806 ddraw->IDirectDraw4_vtbl = &ddraw4_vtbl;
5807 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5808 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5809 ddraw->IDirect3D3_vtbl = &d3d3_vtbl;
5810 ddraw->IDirect3D7_vtbl = &d3d7_vtbl;
5811 ddraw->device_parent_vtbl = &ddraw_wined3d_device_parent_vtbl;
5812 ddraw->numIfaces = 1;
5813 ddraw->ref7 = 1;
5815 /* See comments in IDirectDrawImpl_CreateNewSurface for a description of
5816 * this field. */
5817 ddraw->ImplType = DefaultSurfaceType;
5819 /* Get the current screen settings. */
5820 hDC = GetDC(0);
5821 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5822 ReleaseDC(0, hDC);
5823 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5824 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5826 if (!LoadWineD3D())
5828 ERR("Failed to load wined3d - broken OpenGL setup?\n");
5829 return DDERR_NODIRECTDRAWSUPPORT;
5832 ddraw->wineD3D = pWineDirect3DCreate(7, (IUnknown *)ddraw);
5833 if (!ddraw->wineD3D)
5835 WARN("Failed to create a wined3d object.\n");
5836 return E_OUTOFMEMORY;
5839 hr = IWineD3D_CreateDevice(ddraw->wineD3D, WINED3DADAPTER_DEFAULT, device_type, NULL, 0,
5840 (IWineD3DDeviceParent *)&ddraw->device_parent_vtbl, &ddraw->wineD3DDevice);
5841 if (FAILED(hr))
5843 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5844 IWineD3D_Release(ddraw->wineD3D);
5845 return hr;
5848 /* Get the amount of video memory */
5849 ddraw->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(ddraw->wineD3DDevice);
5851 list_init(&ddraw->surface_list);
5853 return DD_OK;