2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
35 { { 0x00010001, 0x00010001 } },
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static struct enum_device_entry
44 char interface_name[100];
45 char device_name[100];
46 const GUID *device_guid;
51 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
53 &IID_IDirect3DTnLHalDevice,
58 "WINE Direct3D7 Hardware acceleration using WineD3D",
60 &IID_IDirect3DHALDevice,
65 "WINE Direct3D7 RGB Software Emulation using WineD3D",
67 &IID_IDirect3DRGBDevice,
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
75 ddraw_null_wined3d_object_destroyed,
78 static inline IDirectDrawImpl *impl_from_IDirectDraw(IDirectDraw *iface)
80 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw_iface);
83 static inline IDirectDrawImpl *impl_from_IDirectDraw2(IDirectDraw2 *iface)
85 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw2_iface);
88 static inline IDirectDrawImpl *impl_from_IDirectDraw4(IDirectDraw4 *iface)
90 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw4_iface);
93 static inline IDirectDrawImpl *impl_from_IDirectDraw7(IDirectDraw7 *iface)
95 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirectDraw7_iface);
98 static inline IDirectDrawImpl *impl_from_IDirect3D(IDirect3D *iface)
100 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D_iface);
103 static inline IDirectDrawImpl *impl_from_IDirect3D2(IDirect3D2 *iface)
105 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D2_iface);
108 static inline IDirectDrawImpl *impl_from_IDirect3D3(IDirect3D3 *iface)
110 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D3_iface);
113 static inline IDirectDrawImpl *impl_from_IDirect3D7(IDirect3D7 *iface)
115 return CONTAINING_RECORD(iface, IDirectDrawImpl, IDirect3D7_iface);
118 /*****************************************************************************
120 *****************************************************************************/
122 /*****************************************************************************
123 * IDirectDraw7::QueryInterface
125 * Queries different interfaces of the DirectDraw object. It can return
126 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
127 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
129 * The returned interface is AddRef()-ed before it's returned
131 * Used for version 1, 2, 4 and 7
134 * refiid: Interface ID asked for
135 * obj: Used to return the interface pointer
138 * S_OK if an interface was found
139 * E_NOINTERFACE if the requested interface wasn't found
141 *****************************************************************************/
142 static HRESULT WINAPI ddraw7_QueryInterface(IDirectDraw7 *iface, REFIID refiid, void **obj)
144 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
146 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
148 /* Can change surface impl type */
149 wined3d_mutex_lock();
151 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
156 wined3d_mutex_unlock();
157 return DDERR_INVALIDPARAMS;
160 /* Check DirectDraw Interfaces */
161 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
162 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
165 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
167 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
169 *obj = &This->IDirectDraw4_iface;
170 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
172 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
174 /* This Interface exists in ddrawex.dll, it is implemented in a wrapper */
175 WARN("IDirectDraw3 is not valid in ddraw.dll\n");
177 wined3d_mutex_unlock();
178 return E_NOINTERFACE;
180 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
182 *obj = &This->IDirectDraw2_iface;
183 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
185 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
187 *obj = &This->IDirectDraw_iface;
188 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
192 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
193 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
194 * who had this idea and why. The older interfaces can query and IDirect3D version
195 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
196 * and messy to implement with the common creation function, so it has been left out here.
198 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
199 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
200 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
201 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
203 /* Check the surface implementation */
204 if (DefaultSurfaceType != SURFACE_OPENGL)
206 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
207 /* Do not abort here, only reject 3D Device creation */
210 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
212 This->d3dversion = 1;
213 *obj = &This->IDirect3D_iface;
214 TRACE(" returning Direct3D interface at %p.\n", *obj);
216 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
218 This->d3dversion = 2;
219 *obj = &This->IDirect3D2_iface;
220 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
222 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
224 This->d3dversion = 3;
225 *obj = &This->IDirect3D3_iface;
226 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
228 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
230 This->d3dversion = 7;
231 *obj = &This->IDirect3D7_iface;
232 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
235 /* Unknown interface */
238 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
239 wined3d_mutex_unlock();
240 return E_NOINTERFACE;
243 IUnknown_AddRef( (IUnknown *) *obj );
244 wined3d_mutex_unlock();
249 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
251 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
253 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
255 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
258 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
260 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
262 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
264 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
267 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
269 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
271 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
273 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
276 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
278 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
280 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
282 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
285 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
287 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
289 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
291 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
294 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
296 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
298 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
300 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
303 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
305 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
307 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
309 return ddraw7_QueryInterface(&This->IDirectDraw7_iface, riid, object);
312 /*****************************************************************************
313 * IDirectDraw7::AddRef
315 * Increases the interfaces refcount, basically
317 * DDraw refcounting is a bit tricky. The different DirectDraw interface
318 * versions have individual refcounts, but the IDirect3D interfaces do not.
319 * All interfaces are from one object, that means calling QueryInterface on an
320 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
321 * IDirectDrawImpl object.
323 * That means all AddRef and Release implementations of IDirectDrawX work
324 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
325 * except of IDirect3D7 which thunks to IDirectDraw7
327 * Returns: The new refcount
329 *****************************************************************************/
330 static ULONG WINAPI ddraw7_AddRef(IDirectDraw7 *iface)
332 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
333 ULONG ref = InterlockedIncrement(&This->ref7);
335 TRACE("%p increasing refcount to %u.\n", This, ref);
337 if(ref == 1) InterlockedIncrement(&This->numIfaces);
342 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
344 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
345 ULONG ref = InterlockedIncrement(&This->ref4);
347 TRACE("%p increasing refcount to %u.\n", This, ref);
349 if (ref == 1) InterlockedIncrement(&This->numIfaces);
354 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
356 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
357 ULONG ref = InterlockedIncrement(&This->ref2);
359 TRACE("%p increasing refcount to %u.\n", This, ref);
361 if (ref == 1) InterlockedIncrement(&This->numIfaces);
366 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
368 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
369 ULONG ref = InterlockedIncrement(&This->ref1);
371 TRACE("%p increasing refcount to %u.\n", This, ref);
373 if (ref == 1) InterlockedIncrement(&This->numIfaces);
378 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
380 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
382 TRACE("iface %p.\n", iface);
384 return ddraw7_AddRef(&This->IDirectDraw7_iface);
387 static ULONG WINAPI d3d3_AddRef(IDirect3D3 *iface)
389 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
391 TRACE("iface %p.\n", iface);
393 return ddraw1_AddRef(&This->IDirectDraw_iface);
396 static ULONG WINAPI d3d2_AddRef(IDirect3D2 *iface)
398 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
400 TRACE("iface %p.\n", iface);
402 return ddraw1_AddRef(&This->IDirectDraw_iface);
405 static ULONG WINAPI d3d1_AddRef(IDirect3D *iface)
407 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
409 TRACE("iface %p.\n", iface);
411 return ddraw1_AddRef(&This->IDirectDraw_iface);
414 void ddraw_destroy_swapchain(IDirectDrawImpl *ddraw)
416 TRACE("Destroying the swapchain.\n");
418 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
419 ddraw->wined3d_swapchain = NULL;
421 if (DefaultSurfaceType == SURFACE_OPENGL)
425 for (i = 0; i < ddraw->numConvertedDecls; ++i)
427 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
429 HeapFree(GetProcessHeap(), 0, ddraw->decls);
430 ddraw->numConvertedDecls = 0;
432 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
434 ERR("Failed to uninit 3D.\n");
438 /* Free the d3d window if one was created. */
439 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
441 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
442 DestroyWindow(ddraw->d3d_window);
443 ddraw->d3d_window = 0;
447 ddraw->d3d_initialized = FALSE;
451 wined3d_device_uninit_gdi(ddraw->wined3d_device);
454 ddraw_set_swapchain_window(ddraw, NULL);
456 TRACE("Swapchain destroyed.\n");
459 /*****************************************************************************
462 * Destroys a ddraw object if all refcounts are 0. This is to share code
463 * between the IDirectDrawX::Release functions
466 * This: DirectDraw object to destroy
468 *****************************************************************************/
469 static void ddraw_destroy(IDirectDrawImpl *This)
471 IDirectDraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, NULL, DDSCL_NORMAL);
472 IDirectDraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
474 /* Destroy the device window if we created one */
475 if(This->devicewindow != 0)
477 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
478 DestroyWindow(This->devicewindow);
479 This->devicewindow = 0;
482 wined3d_mutex_lock();
483 list_remove(&This->ddraw_list_entry);
484 wined3d_mutex_unlock();
486 if (This->wined3d_swapchain)
487 ddraw_destroy_swapchain(This);
488 wined3d_device_decref(This->wined3d_device);
489 wined3d_decref(This->wined3d);
491 /* Now free the object */
492 HeapFree(GetProcessHeap(), 0, This);
495 /*****************************************************************************
496 * IDirectDraw7::Release
498 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
500 * Returns: The new refcount
501 *****************************************************************************/
502 static ULONG WINAPI ddraw7_Release(IDirectDraw7 *iface)
504 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
505 ULONG ref = InterlockedDecrement(&This->ref7);
507 TRACE("%p decreasing refcount to %u.\n", This, ref);
509 if (!ref && !InterlockedDecrement(&This->numIfaces))
515 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
517 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
518 ULONG ref = InterlockedDecrement(&This->ref4);
520 TRACE("%p decreasing refcount to %u.\n", This, ref);
522 if (!ref && !InterlockedDecrement(&This->numIfaces))
528 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
530 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
531 ULONG ref = InterlockedDecrement(&This->ref2);
533 TRACE("%p decreasing refcount to %u.\n", This, ref);
535 if (!ref && !InterlockedDecrement(&This->numIfaces))
541 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
543 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
544 ULONG ref = InterlockedDecrement(&This->ref1);
546 TRACE("%p decreasing refcount to %u.\n", This, ref);
548 if (!ref && !InterlockedDecrement(&This->numIfaces))
554 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
556 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
558 TRACE("iface %p.\n", iface);
560 return ddraw7_Release(&This->IDirectDraw7_iface);
563 static ULONG WINAPI d3d3_Release(IDirect3D3 *iface)
565 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
567 TRACE("iface %p.\n", iface);
569 return ddraw1_Release(&This->IDirectDraw_iface);
572 static ULONG WINAPI d3d2_Release(IDirect3D2 *iface)
574 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
576 TRACE("iface %p.\n", iface);
578 return ddraw1_Release(&This->IDirectDraw_iface);
581 static ULONG WINAPI d3d1_Release(IDirect3D *iface)
583 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
585 TRACE("iface %p.\n", iface);
587 return ddraw1_Release(&This->IDirectDraw_iface);
590 /*****************************************************************************
591 * IDirectDraw methods
592 *****************************************************************************/
594 static HRESULT ddraw_set_focus_window(IDirectDrawImpl *ddraw, HWND window)
596 /* FIXME: This looks wrong, exclusive mode should imply a destination
598 if ((ddraw->cooperative_level & DDSCL_EXCLUSIVE) && ddraw->dest_window)
600 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET.\n");
601 return DDERR_HWNDALREADYSET;
604 ddraw->focuswindow = window;
606 /* Use the focus window for drawing too. */
607 ddraw->dest_window = ddraw->focuswindow;
609 /* Destroy the device window, if we have one. */
610 if (ddraw->devicewindow)
612 DestroyWindow(ddraw->devicewindow);
613 ddraw->devicewindow = NULL;
619 static HRESULT ddraw_attach_d3d_device(IDirectDrawImpl *ddraw,
620 WINED3DPRESENT_PARAMETERS *presentation_parameters)
622 HWND window = presentation_parameters->hDeviceWindow;
625 TRACE("ddraw %p.\n", ddraw);
627 if (!window || window == GetDesktopWindow())
629 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
630 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
631 NULL, NULL, NULL, NULL);
634 ERR("Failed to create window, last error %#x.\n", GetLastError());
638 ShowWindow(window, SW_HIDE); /* Just to be sure */
639 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
641 presentation_parameters->hDeviceWindow = window;
645 TRACE("Using existing window %p for Direct3D rendering.\n", window);
647 ddraw->d3d_window = window;
649 /* Set this NOW, otherwise creating the depth stencil surface will cause a
650 * recursive loop until ram or emulated video memory is full. */
651 ddraw->d3d_initialized = TRUE;
652 hr = wined3d_device_init_3d(ddraw->wined3d_device, presentation_parameters);
655 ddraw->d3d_initialized = FALSE;
659 ddraw->declArraySize = 2;
660 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
663 ERR("Error allocating an array for the converted vertex decls.\n");
664 ddraw->declArraySize = 0;
665 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
666 return E_OUTOFMEMORY;
669 TRACE("Successfully initialized 3D.\n");
674 static HRESULT ddraw_create_swapchain(IDirectDrawImpl *ddraw, HWND window, BOOL windowed)
676 WINED3DPRESENT_PARAMETERS presentation_parameters;
677 struct wined3d_display_mode mode;
678 HRESULT hr = WINED3D_OK;
680 /* FIXME: wined3d_get_adapter_display_mode() would be more appropriate
681 * here, since we don't actually have a swapchain yet, but
682 * wined3d_device_get_display_mode() has some special handling for color
684 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &mode);
687 ERR("Failed to get display mode.\n");
691 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
692 presentation_parameters.BackBufferWidth = mode.width;
693 presentation_parameters.BackBufferHeight = mode.height;
694 presentation_parameters.BackBufferFormat = mode.format_id;
695 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
696 presentation_parameters.hDeviceWindow = window;
697 presentation_parameters.Windowed = windowed;
699 if (DefaultSurfaceType == SURFACE_OPENGL)
700 hr = ddraw_attach_d3d_device(ddraw, &presentation_parameters);
702 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &presentation_parameters);
706 ERR("Failed to create swapchain, hr %#x.\n", hr);
710 if (FAILED(hr = wined3d_device_get_swapchain(ddraw->wined3d_device, 0, &ddraw->wined3d_swapchain)))
712 ERR("Failed to get swapchain, hr %#x.\n", hr);
713 ddraw->wined3d_swapchain = NULL;
717 ddraw_set_swapchain_window(ddraw, window);
722 /*****************************************************************************
723 * IDirectDraw7::SetCooperativeLevel
725 * Sets the cooperative level for the DirectDraw object, and the window
726 * assigned to it. The cooperative level determines the general behavior
727 * of the DirectDraw application
729 * Warning: This is quite tricky, as it's not really documented which
730 * cooperative levels can be combined with each other. If a game fails
731 * after this function, try to check the cooperative levels passed on
732 * Windows, and if it returns something different.
734 * If you think that this function caused the failure because it writes a
735 * fixme, be sure to run again with a +ddraw trace.
737 * What is known about cooperative levels (See the ddraw modes test):
738 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
739 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
740 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
741 * DDSCL_FULLSCREEN can be activated
742 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
744 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
745 * DDSCL_SETFOCUSWINDOW (partially),
746 * DDSCL_MULTITHREADED (work in progress)
748 * Unhandled flags, which should be implemented
749 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
750 * expect any difference to a normal window for wine)
751 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
752 * rendering (Possible test case: Half-Life)
754 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
756 * These don't seem very important for wine:
757 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
760 * DD_OK if the cooperative level was set successfully
761 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
762 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
763 * (Probably others too, have to investigate)
765 *****************************************************************************/
766 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
768 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
772 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
773 DDRAW_dump_cooperativelevel(cooplevel);
775 wined3d_mutex_lock();
777 /* Get the old window */
778 window = This->dest_window;
780 /* Tests suggest that we need one of them: */
781 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
785 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
786 wined3d_mutex_unlock();
787 return DDERR_INVALIDPARAMS;
790 /* Handle those levels first which set various hwnds */
791 if(cooplevel & DDSCL_SETFOCUSWINDOW)
793 /* This isn't compatible with a lot of flags */
794 if(cooplevel & ( DDSCL_MULTITHREADED |
795 DDSCL_CREATEDEVICEWINDOW |
800 DDSCL_SETDEVICEWINDOW |
805 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
806 wined3d_mutex_unlock();
807 return DDERR_INVALIDPARAMS;
810 hr = ddraw_set_focus_window(This, hwnd);
811 wined3d_mutex_unlock();
815 if(cooplevel & DDSCL_EXCLUSIVE)
817 if( !(cooplevel & DDSCL_FULLSCREEN) || !hwnd )
819 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN and a window\n", This);
820 wined3d_mutex_unlock();
821 return DDERR_INVALIDPARAMS;
825 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
826 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
827 wined3d_device_release_focus_window(This->wined3d_device);
829 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
831 if (This->cooperative_level & DDSCL_FULLSCREEN)
832 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
834 if (cooplevel & DDSCL_FULLSCREEN)
836 struct wined3d_display_mode display_mode;
838 wined3d_get_adapter_display_mode(This->wined3d, WINED3DADAPTER_DEFAULT, &display_mode);
839 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
840 display_mode.width, display_mode.height);
844 if ((cooplevel & DDSCL_EXCLUSIVE)
845 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
847 hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
850 ERR("Failed to acquire focus window, hr %#x.\n", hr);
851 wined3d_mutex_unlock();
856 /* Don't override focus windows or private device windows */
857 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
858 This->dest_window = hwnd;
860 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
862 /* Don't create a device window if a focus window is set */
863 if( !(This->focuswindow) )
865 HWND devicewindow = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DDraw device window",
866 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
867 NULL, NULL, NULL, NULL);
870 ERR("Failed to create window, last error %#x.\n", GetLastError());
871 wined3d_mutex_unlock();
875 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
876 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
878 This->devicewindow = devicewindow;
879 This->dest_window = devicewindow;
883 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
884 wined3d_device_set_multithreaded(This->wined3d_device);
886 if (This->wined3d_swapchain)
887 ddraw_destroy_swapchain(This);
888 if (FAILED(hr = ddraw_create_swapchain(This, This->dest_window, !(cooplevel & DDSCL_FULLSCREEN))))
889 ERR("Failed to create swapchain, hr %#x.\n", hr);
891 /* Unhandled flags */
892 if(cooplevel & DDSCL_ALLOWREBOOT)
893 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
894 if(cooplevel & DDSCL_ALLOWMODEX)
895 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
896 if(cooplevel & DDSCL_FPUSETUP)
897 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
899 /* Store the cooperative_level */
900 This->cooperative_level = cooplevel;
901 TRACE("SetCooperativeLevel retuning DD_OK\n");
902 wined3d_mutex_unlock();
907 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
909 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
911 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
913 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
916 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
918 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
920 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
922 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
925 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
927 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
929 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
931 return ddraw7_SetCooperativeLevel(&This->IDirectDraw7_iface, window, flags);
934 /*****************************************************************************
936 * Helper function for SetDisplayMode and RestoreDisplayMode
938 * Implements DirectDraw's SetDisplayMode, but ignores the value of
939 * ForceRefreshRate, since it is already handled by
940 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
941 * without worrying that ForceRefreshRate will override the refresh rate. For
942 * argument and return value documentation, see
943 * ddraw7_SetDisplayMode.
945 *****************************************************************************/
946 static HRESULT ddraw_set_display_mode(IDirectDrawImpl *ddraw, DWORD Width, DWORD Height,
947 DWORD BPP, DWORD RefreshRate, DWORD Flags)
949 struct wined3d_display_mode mode;
950 enum wined3d_format_id format;
953 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
954 Height, BPP, RefreshRate, Flags);
956 wined3d_mutex_lock();
957 if( !Width || !Height )
959 ERR("Width %u, Height %u, what to do?\n", Width, Height);
960 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
961 wined3d_mutex_unlock();
967 case 8: format = WINED3DFMT_P8_UINT; break;
968 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
969 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
970 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
971 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
972 default: format = WINED3DFMT_UNKNOWN; break;
975 if (FAILED(hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &mode)))
977 ERR("Failed to get current display mode, hr %#x.\n", hr);
979 else if (mode.width == Width
980 && mode.height == Height
981 && mode.format_id == format
982 && mode.refresh_rate == RefreshRate)
984 TRACE("Skipping redundant mode setting call.\n");
985 wined3d_mutex_unlock();
989 /* Check the exclusive mode
990 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
991 return DDERR_NOEXCLUSIVEMODE;
992 * This is WRONG. Don't know if the SDK is completely
993 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
994 * is returned, but Half-Life 1.1.1.1 (Steam version)
999 mode.height = Height;
1000 mode.refresh_rate = RefreshRate;
1001 mode.format_id = format;
1003 /* TODO: The possible return values from msdn suggest that
1004 * the screen mode can't be changed if a surface is locked
1005 * or some drawing is in progress
1008 /* TODO: Lose the primary surface */
1009 hr = wined3d_device_set_display_mode(ddraw->wined3d_device, 0, &mode);
1011 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
1012 SetWindowPos(ddraw->dest_window, HWND_TOP, 0, 0, Width, Height, SWP_SHOWWINDOW | SWP_NOACTIVATE);
1014 wined3d_mutex_unlock();
1018 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1023 /*****************************************************************************
1024 * IDirectDraw7::SetDisplayMode
1026 * Sets the display screen resolution, color depth and refresh frequency
1027 * when in fullscreen mode (in theory).
1028 * Possible return values listed in the SDK suggest that this method fails
1029 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1030 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1031 * It seems to be valid to pass 0 for With and Height, this has to be tested
1032 * It could mean that the current video mode should be left as-is. (But why
1036 * Height, Width: Screen dimension
1037 * BPP: Color depth in Bits per pixel
1038 * Refreshrate: Screen refresh rate
1039 * Flags: Other stuff
1044 *****************************************************************************/
1045 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
1046 DWORD BPP, DWORD RefreshRate, DWORD Flags)
1048 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1050 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1051 iface, Width, Height, BPP, RefreshRate, Flags);
1053 if (force_refresh_rate != 0)
1055 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1056 RefreshRate, force_refresh_rate);
1057 RefreshRate = force_refresh_rate;
1060 return ddraw_set_display_mode(This, Width, Height, BPP, RefreshRate, Flags);
1063 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1064 DWORD bpp, DWORD refresh_rate, DWORD flags)
1066 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1068 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1069 iface, width, height, bpp, refresh_rate, flags);
1071 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1074 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1075 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1077 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1079 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1080 iface, width, height, bpp, refresh_rate, flags);
1082 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1085 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1087 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1089 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1091 return ddraw7_SetDisplayMode(&This->IDirectDraw7_iface, width, height, bpp, 0, 0);
1094 /*****************************************************************************
1095 * IDirectDraw7::RestoreDisplayMode
1097 * Restores the display mode to what it was at creation time. Basically.
1099 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1100 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1101 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1102 * -> DD_1 is released. The screen should be left at 1024x768x32.
1103 * -> DD_2 is released. The screen should be set to 1400x1050x32
1104 * This case is unhandled right now, but Empire Earth does it this way.
1105 * (But perhaps there is something in SetCooperativeLevel to prevent this)
1107 * The msdn says that this method resets the display mode to what it was before
1108 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1112 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1114 *****************************************************************************/
1115 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1117 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1119 TRACE("iface %p.\n", iface);
1121 return ddraw_set_display_mode(This, This->orig_width, This->orig_height, This->orig_bpp, 0, 0);
1124 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1126 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1128 TRACE("iface %p.\n", iface);
1130 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1133 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1135 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1137 TRACE("iface %p.\n", iface);
1139 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1142 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1144 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1146 TRACE("iface %p.\n", iface);
1148 return ddraw7_RestoreDisplayMode(&This->IDirectDraw7_iface);
1151 /*****************************************************************************
1152 * IDirectDraw7::GetCaps
1154 * Returns the drives capabilities
1156 * Used for version 1, 2, 4 and 7
1159 * DriverCaps: Structure to write the Hardware accelerated caps to
1160 * HelCaps: Structure to write the emulation caps to
1163 * This implementation returns DD_OK only
1165 *****************************************************************************/
1166 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1168 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1170 WINED3DCAPS winecaps;
1172 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1174 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1176 /* One structure must be != NULL */
1177 if( (!DriverCaps) && (!HELCaps) )
1179 ERR("(%p) Invalid params to ddraw7_GetCaps\n", This);
1180 return DDERR_INVALIDPARAMS;
1183 memset(&caps, 0, sizeof(caps));
1184 memset(&winecaps, 0, sizeof(winecaps));
1185 caps.dwSize = sizeof(caps);
1187 wined3d_mutex_lock();
1188 hr = wined3d_device_get_device_caps(This->wined3d_device, &winecaps);
1191 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1192 wined3d_mutex_unlock();
1196 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1197 wined3d_mutex_unlock();
1199 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1203 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
1204 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
1205 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
1206 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
1207 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
1208 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
1209 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
1210 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
1211 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
1212 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
1213 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
1214 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
1215 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
1216 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
1217 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
1219 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1222 if(DefaultSurfaceType == SURFACE_GDI) {
1223 caps.dwCaps &= ~DDCAPS_3D;
1224 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1226 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
1227 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1228 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
1233 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1234 if (TRACE_ON(ddraw))
1236 TRACE("Driver Caps :\n");
1237 DDRAW_dump_DDCAPS(DriverCaps);
1243 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1244 if (TRACE_ON(ddraw))
1246 TRACE("HEL Caps :\n");
1247 DDRAW_dump_DDCAPS(HELCaps);
1254 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1256 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1258 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1260 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1263 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1265 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1267 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1269 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1272 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1274 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1276 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1278 return ddraw7_GetCaps(&This->IDirectDraw7_iface, driver_caps, hel_caps);
1281 /*****************************************************************************
1282 * IDirectDraw7::Compact
1284 * No idea what it does, MSDN says it's not implemented.
1287 * DD_OK, but this is unchecked
1289 *****************************************************************************/
1290 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1292 TRACE("iface %p.\n", iface);
1297 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1299 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1301 TRACE("iface %p.\n", iface);
1303 return ddraw7_Compact(&This->IDirectDraw7_iface);
1306 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1308 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1310 TRACE("iface %p.\n", iface);
1312 return ddraw7_Compact(&This->IDirectDraw7_iface);
1315 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1317 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1319 TRACE("iface %p.\n", iface);
1321 return ddraw7_Compact(&This->IDirectDraw7_iface);
1324 /*****************************************************************************
1325 * IDirectDraw7::GetDisplayMode
1327 * Returns information about the current display mode
1329 * Exists in Version 1, 2, 4 and 7
1332 * DDSD: Address of a surface description structure to write the info to
1337 *****************************************************************************/
1338 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1340 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1341 struct wined3d_display_mode mode;
1345 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1347 wined3d_mutex_lock();
1348 /* This seems sane */
1351 wined3d_mutex_unlock();
1352 return DDERR_INVALIDPARAMS;
1355 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
1356 * so one method can be used for all versions (Hopefully) */
1357 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
1360 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
1361 wined3d_mutex_unlock();
1365 Size = DDSD->dwSize;
1366 memset(DDSD, 0, Size);
1368 DDSD->dwSize = Size;
1369 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1370 DDSD->dwWidth = mode.width;
1371 DDSD->dwHeight = mode.height;
1372 DDSD->u2.dwRefreshRate = 60;
1373 DDSD->ddsCaps.dwCaps = 0;
1374 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1375 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1376 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1380 TRACE("Returning surface desc :\n");
1381 DDRAW_dump_surface_desc(DDSD);
1384 wined3d_mutex_unlock();
1389 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1391 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1393 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1395 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, surface_desc);
1398 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1400 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1402 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1404 /* FIXME: Test sizes, properly convert surface_desc */
1405 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1408 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1410 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1412 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1414 /* FIXME: Test sizes, properly convert surface_desc */
1415 return ddraw7_GetDisplayMode(&This->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1418 /*****************************************************************************
1419 * IDirectDraw7::GetFourCCCodes
1421 * Returns an array of supported FourCC codes.
1423 * Exists in Version 1, 2, 4 and 7
1426 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1427 * of enumerated codes
1428 * Codes: Pointer to an array of DWORDs where the supported codes are written
1432 * Always returns DD_OK, as it's a stub for now
1434 *****************************************************************************/
1435 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1437 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1438 static const enum wined3d_format_id formats[] =
1440 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1441 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1442 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1444 struct wined3d_display_mode mode;
1445 DWORD count = 0, i, outsize;
1448 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1450 wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
1452 outsize = NumCodes && Codes ? *NumCodes : 0;
1454 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1456 hr = wined3d_check_device_format(This->wined3d, WINED3DADAPTER_DEFAULT, WINED3DDEVTYPE_HAL,
1457 mode.format_id, 0, WINED3DRTYPE_SURFACE, formats[i], DefaultSurfaceType);
1460 if (count < outsize)
1461 Codes[count] = formats[i];
1466 TRACE("Returning %u FourCC codes\n", count);
1473 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1475 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1477 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1479 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1482 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1484 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1486 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1488 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1491 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1493 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1495 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1497 return ddraw7_GetFourCCCodes(&This->IDirectDraw7_iface, codes_count, codes);
1500 /*****************************************************************************
1501 * IDirectDraw7::GetMonitorFrequency
1503 * Returns the monitor's frequency
1505 * Exists in Version 1, 2, 4 and 7
1508 * Freq: Pointer to a DWORD to write the frequency to
1511 * Always returns DD_OK
1513 *****************************************************************************/
1514 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *Freq)
1516 FIXME("iface %p, frequency %p stub!\n", iface, Freq);
1518 /* Ideally this should be in WineD3D, as it concerns the screen setup,
1519 * but for now this should make the games happy
1525 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1527 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1529 TRACE("iface %p, frequency %p.\n", iface, frequency);
1531 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1534 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1536 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1538 TRACE("iface %p, frequency %p.\n", iface, frequency);
1540 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1543 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1545 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1547 TRACE("iface %p, frequency %p.\n", iface, frequency);
1549 return ddraw7_GetMonitorFrequency(&This->IDirectDraw7_iface, frequency);
1552 /*****************************************************************************
1553 * IDirectDraw7::GetVerticalBlankStatus
1555 * Returns the Vertical blank status of the monitor. This should be in WineD3D
1556 * too basically, but as it's a semi stub, I didn't create a function there
1559 * status: Pointer to a BOOL to be filled with the vertical blank status
1563 * DDERR_INVALIDPARAMS if status is NULL
1565 *****************************************************************************/
1566 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1568 static BOOL fake_vblank;
1570 TRACE("iface %p, status %p.\n", iface, status);
1573 return DDERR_INVALIDPARAMS;
1575 *status = fake_vblank;
1576 fake_vblank = !fake_vblank;
1581 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1583 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1585 TRACE("iface %p, status %p.\n", iface, status);
1587 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1590 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1592 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1594 TRACE("iface %p, status %p.\n", iface, status);
1596 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1599 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1601 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1603 TRACE("iface %p, status %p.\n", iface, status);
1605 return ddraw7_GetVerticalBlankStatus(&This->IDirectDraw7_iface, status);
1608 /*****************************************************************************
1609 * IDirectDraw7::GetAvailableVidMem
1611 * Returns the total and free video memory
1614 * Caps: Specifies the memory type asked for
1615 * total: Pointer to a DWORD to be filled with the total memory
1616 * free: Pointer to a DWORD to be filled with the free memory
1620 * DDERR_INVALIDPARAMS of free and total are NULL
1622 *****************************************************************************/
1623 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1626 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1629 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1633 TRACE("(%p) Asked for memory with description: ", This);
1634 DDRAW_dump_DDSCAPS2(Caps);
1636 wined3d_mutex_lock();
1638 /* Todo: System memory vs local video memory vs non-local video memory
1639 * The MSDN also mentions differences between texture memory and other
1640 * resources, but that's not important
1643 if( (!total) && (!free) )
1645 wined3d_mutex_unlock();
1646 return DDERR_INVALIDPARAMS;
1650 *free = wined3d_device_get_available_texture_mem(This->wined3d_device);
1653 struct wined3d_adapter_identifier desc = {0};
1655 hr = wined3d_get_adapter_identifier(This->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1656 *total = desc.video_memory;
1659 wined3d_mutex_unlock();
1664 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1665 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1667 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1669 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1671 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, caps, total, free);
1674 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1675 DDSCAPS *caps, DWORD *total, DWORD *free)
1677 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1680 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1682 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1683 return ddraw7_GetAvailableVidMem(&This->IDirectDraw7_iface, &caps2, total, free);
1686 /*****************************************************************************
1687 * IDirectDraw7::Initialize
1689 * Initializes a DirectDraw interface.
1692 * GUID: Interface identifier. Well, don't know what this is really good
1696 * Returns DD_OK on the first call,
1697 * DDERR_ALREADYINITIALIZED on repeated calls
1699 *****************************************************************************/
1700 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1702 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1704 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1706 if (This->initialized)
1707 return DDERR_ALREADYINITIALIZED;
1709 /* FIXME: To properly take the GUID into account we should call
1710 * ddraw_init() here instead of in DDRAW_Create(). */
1712 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1714 This->initialized = TRUE;
1718 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1720 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1722 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1724 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1727 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1729 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1731 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1733 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1736 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1738 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1740 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1742 return ddraw7_Initialize(&This->IDirectDraw7_iface, guid);
1745 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1747 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1749 return DDERR_ALREADYINITIALIZED;
1752 /*****************************************************************************
1753 * IDirectDraw7::FlipToGDISurface
1755 * "Makes the surface that the GDI writes to the primary surface"
1756 * Looks like some windows specific thing we don't have to care about.
1757 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1758 * show error boxes ;)
1759 * Well, just return DD_OK.
1762 * Always returns DD_OK
1764 *****************************************************************************/
1765 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1767 FIXME("iface %p stub!\n", iface);
1772 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1774 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1776 TRACE("iface %p.\n", iface);
1778 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1781 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1783 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1785 TRACE("iface %p.\n", iface);
1787 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1790 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1792 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1794 TRACE("iface %p.\n", iface);
1796 return ddraw7_FlipToGDISurface(&This->IDirectDraw7_iface);
1799 /*****************************************************************************
1800 * IDirectDraw7::WaitForVerticalBlank
1802 * This method allows applications to get in sync with the vertical blank
1804 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1805 * redraw the screen, most likely because of this stub
1808 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1809 * or DDWAITVB_BLOCKEND
1810 * h: Not used, according to MSDN
1813 * Always returns DD_OK
1815 *****************************************************************************/
1816 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1820 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1822 /* This function is called often, so print the fixme only once */
1825 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1829 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1830 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1831 return DDERR_UNSUPPORTED; /* unchecked */
1836 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1838 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1840 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1842 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1845 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1847 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1849 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1851 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1854 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1856 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1858 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1860 return ddraw7_WaitForVerticalBlank(&This->IDirectDraw7_iface, flags, event);
1863 /*****************************************************************************
1864 * IDirectDraw7::GetScanLine
1866 * Returns the scan line that is being drawn on the monitor
1869 * Scanline: Address to write the scan line value to
1872 * Always returns DD_OK
1874 *****************************************************************************/
1875 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1877 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1878 struct wined3d_display_mode mode;
1879 static DWORD cur_scanline;
1880 static BOOL hide = FALSE;
1882 TRACE("iface %p, line %p.\n", iface, Scanline);
1884 /* This function is called often, so print the fixme only once */
1887 FIXME("iface %p, line %p partial stub!\n", iface, Scanline);
1891 wined3d_mutex_lock();
1892 wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
1893 wined3d_mutex_unlock();
1895 /* Fake the line sweeping of the monitor */
1896 /* FIXME: We should synchronize with a source to keep the refresh rate */
1897 *Scanline = cur_scanline++;
1898 /* Assume 20 scan lines in the vertical blank */
1899 if (cur_scanline >= mode.height + 20)
1905 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1907 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1909 TRACE("iface %p, line %p.\n", iface, line);
1911 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1914 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1916 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
1918 TRACE("iface %p, line %p.\n", iface, line);
1920 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1923 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1925 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
1927 TRACE("iface %p, line %p.\n", iface, line);
1929 return ddraw7_GetScanLine(&This->IDirectDraw7_iface, line);
1932 /*****************************************************************************
1933 * IDirectDraw7::TestCooperativeLevel
1935 * Informs the application about the state of the video adapter, depending
1936 * on the cooperative level
1939 * DD_OK if the device is in a sane state
1940 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1941 * if the state is not correct(See below)
1943 *****************************************************************************/
1944 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1946 TRACE("iface %p.\n", iface);
1951 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
1953 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1955 TRACE("iface %p.\n", iface);
1957 return ddraw7_TestCooperativeLevel(&This->IDirectDraw7_iface);
1960 /*****************************************************************************
1961 * IDirectDraw7::GetGDISurface
1963 * Returns the surface that GDI is treating as the primary surface.
1964 * For Wine this is the front buffer
1967 * GDISurface: Address to write the surface pointer to
1970 * DD_OK if the surface was found
1971 * DDERR_NOTFOUND if the GDI surface wasn't found
1973 *****************************************************************************/
1974 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
1976 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
1978 TRACE("iface %p, surface %p.\n", iface, GDISurface);
1980 wined3d_mutex_lock();
1982 if (!(*GDISurface = &This->primary->IDirectDrawSurface7_iface))
1984 WARN("Primary not created yet.\n");
1985 wined3d_mutex_unlock();
1986 return DDERR_NOTFOUND;
1988 IDirectDrawSurface7_AddRef(*GDISurface);
1990 wined3d_mutex_unlock();
1995 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
1997 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
1998 IDirectDrawSurface7 *surface7;
1999 IDirectDrawSurfaceImpl *surface_impl;
2002 TRACE("iface %p, surface %p.\n", iface, surface);
2004 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2010 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2011 *surface = &surface_impl->IDirectDrawSurface4_iface;
2012 IDirectDrawSurface4_AddRef(*surface);
2013 IDirectDrawSurface7_Release(surface7);
2018 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2020 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2021 IDirectDrawSurface7 *surface7;
2022 IDirectDrawSurfaceImpl *surface_impl;
2025 TRACE("iface %p, surface %p.\n", iface, surface);
2027 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2033 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2034 *surface = &surface_impl->IDirectDrawSurface_iface;
2035 IDirectDrawSurface_AddRef(*surface);
2036 IDirectDrawSurface7_Release(surface7);
2041 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2043 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2044 IDirectDrawSurface7 *surface7;
2045 IDirectDrawSurfaceImpl *surface_impl;
2048 TRACE("iface %p, surface %p.\n", iface, surface);
2050 hr = ddraw7_GetGDISurface(&This->IDirectDraw7_iface, &surface7);
2056 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2057 *surface = &surface_impl->IDirectDrawSurface_iface;
2058 IDirectDrawSurface_AddRef(*surface);
2059 IDirectDrawSurface7_Release(surface7);
2064 struct displaymodescallback_context
2066 LPDDENUMMODESCALLBACK func;
2070 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2072 struct displaymodescallback_context *cbcontext = context;
2075 DDSD2_to_DDSD(surface_desc, &desc);
2076 return cbcontext->func(&desc, cbcontext->context);
2079 /*****************************************************************************
2080 * IDirectDraw7::EnumDisplayModes
2082 * Enumerates the supported Display modes. The modes can be filtered with
2083 * the DDSD parameter.
2086 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2087 * versions (3 and older?) this is reserved and must be 0.
2088 * DDSD: Surface description to filter the modes
2089 * Context: Pointer passed back to the callback function
2090 * cb: Application-provided callback function
2094 * DDERR_INVALIDPARAMS if the callback wasn't set
2096 *****************************************************************************/
2097 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2098 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2100 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2101 struct wined3d_display_mode *enum_modes = NULL;
2102 struct wined3d_display_mode mode;
2103 unsigned int modenum, fmt;
2104 DDSURFACEDESC2 callback_sd;
2105 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2106 DDPIXELFORMAT pixelformat;
2108 static const enum wined3d_format_id checkFormatList[] =
2110 WINED3DFMT_B8G8R8X8_UNORM,
2111 WINED3DFMT_B5G6R5_UNORM,
2115 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2116 iface, Flags, DDSD, Context, cb);
2119 return DDERR_INVALIDPARAMS;
2121 wined3d_mutex_lock();
2122 if(!(Flags & DDEDM_REFRESHRATES))
2124 enum_mode_array_size = 16;
2125 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2128 ERR("Out of memory\n");
2129 wined3d_mutex_unlock();
2130 return DDERR_OUTOFMEMORY;
2134 pixelformat.dwSize = sizeof(pixelformat);
2135 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2138 while (wined3d_enum_adapter_modes(This->wined3d, WINED3DADAPTER_DEFAULT,
2139 checkFormatList[fmt], modenum++, &mode) == WINED3D_OK)
2141 PixelFormat_WineD3DtoDD(&pixelformat, mode.format_id);
2144 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2146 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2148 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2150 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2151 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2155 if(!(Flags & DDEDM_REFRESHRATES))
2157 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2158 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2159 * to be reduced to a single unique result in such case.
2164 for (i = 0; i < enum_mode_count; i++)
2166 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2167 && enum_modes[i].format_id == mode.format_id)
2177 memset(&callback_sd, 0, sizeof(callback_sd));
2178 callback_sd.dwSize = sizeof(callback_sd);
2179 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2181 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2182 if (Flags & DDEDM_REFRESHRATES)
2183 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2185 callback_sd.dwWidth = mode.width;
2186 callback_sd.dwHeight = mode.height;
2188 callback_sd.u4.ddpfPixelFormat=pixelformat;
2190 /* Calc pitch and DWORD align like MSDN says */
2191 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2192 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2194 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2195 callback_sd.u2.dwRefreshRate);
2197 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2199 TRACE("Application asked to terminate the enumeration\n");
2200 HeapFree(GetProcessHeap(), 0, enum_modes);
2201 wined3d_mutex_unlock();
2205 if(!(Flags & DDEDM_REFRESHRATES))
2207 if (enum_mode_count == enum_mode_array_size)
2209 struct wined3d_display_mode *new_enum_modes;
2211 enum_mode_array_size *= 2;
2212 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2213 sizeof(*new_enum_modes) * enum_mode_array_size);
2214 if (!new_enum_modes)
2216 ERR("Out of memory\n");
2217 HeapFree(GetProcessHeap(), 0, enum_modes);
2218 wined3d_mutex_unlock();
2219 return DDERR_OUTOFMEMORY;
2222 enum_modes = new_enum_modes;
2225 enum_modes[enum_mode_count++] = mode;
2230 TRACE("End of enumeration\n");
2231 HeapFree(GetProcessHeap(), 0, enum_modes);
2232 wined3d_mutex_unlock();
2237 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2238 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2240 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2242 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2243 iface, flags, surface_desc, context, callback);
2245 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags, surface_desc, context, callback);
2248 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2249 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2251 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
2252 struct displaymodescallback_context cbcontext;
2253 DDSURFACEDESC2 surface_desc2;
2255 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2256 iface, flags, surface_desc, context, callback);
2258 cbcontext.func = callback;
2259 cbcontext.context = context;
2261 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2262 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags,
2263 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2266 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2267 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2269 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
2270 struct displaymodescallback_context cbcontext;
2271 DDSURFACEDESC2 surface_desc2;
2273 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2274 iface, flags, surface_desc, context, callback);
2276 cbcontext.func = callback;
2277 cbcontext.context = context;
2279 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2280 return ddraw7_EnumDisplayModes(&This->IDirectDraw7_iface, flags,
2281 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2284 /*****************************************************************************
2285 * IDirectDraw7::EvaluateMode
2287 * Used with IDirectDraw7::StartModeTest to test video modes.
2288 * EvaluateMode is used to pass or fail a mode, and continue with the next
2292 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2293 * Timeout: Returns the amount of seconds left before the mode would have
2294 * been failed automatically
2297 * This implementation always DD_OK, because it's a stub
2299 *****************************************************************************/
2300 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2302 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2304 /* When implementing this, implement it in WineD3D */
2309 /*****************************************************************************
2310 * IDirectDraw7::GetDeviceIdentifier
2312 * Returns the device identifier, which gives information about the driver
2313 * Our device identifier is defined at the beginning of this file.
2316 * DDDI: Address for the returned structure
2317 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2320 * On success it returns DD_OK
2321 * DDERR_INVALIDPARAMS if DDDI is NULL
2323 *****************************************************************************/
2324 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2325 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2327 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2330 return DDERR_INVALIDPARAMS;
2332 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2333 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2334 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2335 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2336 * bytes too long. So only copy the relevant part of the structure
2339 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2343 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2344 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2346 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2347 DDDEVICEIDENTIFIER2 identifier2;
2350 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2352 hr = ddraw7_GetDeviceIdentifier(&This->IDirectDraw7_iface, &identifier2, flags);
2353 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2358 /*****************************************************************************
2359 * IDirectDraw7::GetSurfaceFromDC
2361 * Returns the Surface for a GDI device context handle.
2362 * Is this related to IDirectDrawSurface::GetDC ???
2365 * hdc: hdc to return the surface for
2366 * Surface: Address to write the surface pointer to
2369 * Always returns DD_OK because it's a stub
2371 *****************************************************************************/
2372 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2373 IDirectDrawSurface7 **Surface)
2375 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
2376 struct wined3d_surface *wined3d_surface;
2379 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2381 if (!Surface) return E_INVALIDARG;
2383 hr = wined3d_device_get_surface_from_dc(This->wined3d_device, hdc, &wined3d_surface);
2386 TRACE("No surface found for dc %p.\n", hdc);
2388 return DDERR_NOTFOUND;
2391 *Surface = wined3d_surface_get_parent(wined3d_surface);
2392 IDirectDrawSurface7_AddRef(*Surface);
2393 TRACE("Returning surface %p.\n", Surface);
2397 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2398 IDirectDrawSurface4 **surface)
2400 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2401 IDirectDrawSurface7 *surface7;
2402 IDirectDrawSurfaceImpl *surface_impl;
2405 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2407 if (!surface) return E_INVALIDARG;
2409 hr = ddraw7_GetSurfaceFromDC(&This->IDirectDraw7_iface, dc, &surface7);
2415 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2416 /* Tests say this is true */
2417 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2418 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2419 IDirectDrawSurface7_Release(surface7);
2424 /*****************************************************************************
2425 * IDirectDraw7::RestoreAllSurfaces
2427 * Calls the restore method of all surfaces
2432 * Always returns DD_OK because it's a stub
2434 *****************************************************************************/
2435 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2437 FIXME("iface %p stub!\n", iface);
2439 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2440 * get their parent and call their restore method. Do not implement
2441 * it in WineD3D, as restoring a surface means re-creating the
2447 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2449 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
2451 TRACE("iface %p.\n", iface);
2453 return ddraw7_RestoreAllSurfaces(&This->IDirectDraw7_iface);
2456 /*****************************************************************************
2457 * IDirectDraw7::StartModeTest
2459 * Tests the specified video modes to update the system registry with
2460 * refresh rate information. StartModeTest starts the mode test,
2461 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2462 * isn't called within 15 seconds, the mode is failed automatically
2464 * As refresh rates are handled by the X server, I don't think this
2465 * Method is important
2468 * Modes: An array of mode specifications
2469 * NumModes: The number of modes in Modes
2470 * Flags: Some flags...
2473 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2474 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2477 *****************************************************************************/
2478 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2480 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2481 iface, Modes, NumModes, Flags);
2483 /* This looks sane */
2484 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2486 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2487 * As it is not, DDERR_TESTFINISHED is returned
2488 * (hopefully that's correct
2490 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2491 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2497 /*****************************************************************************
2498 * ddraw_create_surface
2500 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2501 * with the passed parameters.
2504 * DDSD: Description of the surface to create
2505 * Surf: Address to store the interface pointer at
2510 *****************************************************************************/
2511 static HRESULT ddraw_create_surface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD,
2512 IDirectDrawSurfaceImpl **ppSurf, UINT level, UINT version)
2516 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2517 This, pDDSD, ppSurf, level);
2519 if (TRACE_ON(ddraw))
2521 TRACE(" (%p) Requesting surface desc :\n", This);
2522 DDRAW_dump_surface_desc(pDDSD);
2525 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && DefaultSurfaceType != SURFACE_OPENGL)
2527 WARN("The application requests a 3D capable surface, but a non-OpenGL surface type was set in the registry.\n");
2528 /* Do not fail surface creation, only fail 3D device creation. */
2531 /* Create the Surface object */
2532 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2535 ERR("(%p) Error allocating memory for a surface\n", This);
2536 return DDERR_OUTOFVIDEOMEMORY;
2539 hr = ddraw_surface_init(*ppSurf, This, pDDSD, level, version);
2542 WARN("Failed to initialize surface, hr %#x.\n", hr);
2543 HeapFree(GetProcessHeap(), 0, *ppSurf);
2547 /* Increase the surface counter, and attach the surface */
2548 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2550 TRACE("Created surface %p.\n", *ppSurf);
2554 /*****************************************************************************
2555 * CreateAdditionalSurfaces
2557 * Creates a new mipmap chain.
2560 * root: Root surface to attach the newly created chain to
2561 * count: number of surfaces to create
2562 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2563 * effects on the caller
2564 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2565 * creates an additional surface without the mipmapping flags
2567 *****************************************************************************/
2569 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2570 IDirectDrawSurfaceImpl *root,
2572 DDSURFACEDESC2 DDSD,
2573 BOOL CubeFaceRoot, UINT version)
2575 UINT i, j, level = 0;
2577 IDirectDrawSurfaceImpl *last = root;
2579 for(i = 0; i < count; i++)
2581 IDirectDrawSurfaceImpl *object2 = NULL;
2583 /* increase the mipmap level, but only if a mipmap is created
2584 * In this case, also halve the size
2586 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2589 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2590 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2591 /* Set the mipmap sublevel flag according to msdn */
2592 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2596 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2598 CubeFaceRoot = FALSE;
2600 hr = ddraw_create_surface(This, &DDSD, &object2, level, version);
2606 /* Add the new surface to the complex attachment array */
2607 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2609 if(last->complex_array[j]) continue;
2610 last->complex_array[j] = object2;
2615 /* Remove the (possible) back buffer cap from the new surface description,
2616 * because only one surface in the flipping chain is a back buffer, one
2617 * is a front buffer, the others are just primary surfaces.
2619 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2624 HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
2629 /*****************************************************************************
2630 * IDirectDraw7::CreateSurface
2632 * Creates a new IDirectDrawSurface object and returns its interface.
2634 * The surface connections with wined3d are a bit tricky. Basically it works
2637 * |------------------------| |-----------------|
2638 * | DDraw surface | | WineD3DSurface |
2640 * | WineD3DSurface |-------------->| |
2641 * | Child |<------------->| Parent |
2642 * |------------------------| |-----------------|
2644 * The DDraw surface is the parent of the wined3d surface, and it releases
2645 * the WineD3DSurface when the ddraw surface is destroyed.
2647 * However, for all surfaces which can be in a container in WineD3D,
2648 * we have to do this. These surfaces are usually complex surfaces,
2649 * so this concerns primary surfaces with a front and a back buffer,
2652 * |------------------------| |-----------------|
2653 * | DDraw surface | | Container |
2655 * | Child |<------------->| Parent |
2656 * | Texture |<------------->| |
2657 * | WineD3DSurface |<----| | Levels |<--|
2658 * | Complex connection | | | | |
2659 * |------------------------| | |-----------------| |
2663 * | |------------------| | |-----------------| |
2664 * | | IParent | |-------->| WineD3DSurface | |
2666 * | | Child |<------------->| Parent | |
2667 * | | | | Container |<--|
2668 * | |------------------| |-----------------| |
2670 * | |----------------------| |
2671 * | | DDraw surface 2 | |
2673 * |<->| Complex root Child | |
2675 * | | WineD3DSurface |<----| |
2676 * | |----------------------| | |
2678 * | |---------------------| | |-----------------| |
2679 * | | IParent | |----->| WineD3DSurface | |
2681 * | | Child |<---------->| Parent | |
2682 * | |---------------------| | Container |<--|
2683 * | |-----------------| |
2685 * | ---More surfaces can follow--- |
2687 * The reason is that the IWineD3DSwapchain(render target container)
2688 * and the IWineD3DTexure(Texture container) release the parents
2689 * of their surface's children, but by releasing the complex root
2690 * the surfaces which are complexly attached to it are destroyed
2691 * too. See IDirectDrawSurface::Release for a more detailed
2695 * DDSD: Description of the surface to create
2696 * Surf: Address to store the interface pointer at
2697 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2698 * aggregation, so it has to be NULL
2702 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2703 * DDERR_* if an error occurs
2705 *****************************************************************************/
2706 static HRESULT CreateSurface(IDirectDrawImpl *ddraw, DDSURFACEDESC2 *DDSD,
2707 IDirectDrawSurfaceImpl **Surf, IUnknown *UnkOuter, UINT version)
2709 IDirectDrawSurfaceImpl *object = NULL;
2710 struct wined3d_display_mode mode;
2712 LONG extra_surfaces = 0;
2713 DDSURFACEDESC2 desc2;
2714 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2716 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, Surf, UnkOuter);
2718 /* Some checks before we start */
2719 if (TRACE_ON(ddraw))
2721 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2722 DDRAW_dump_surface_desc(DDSD);
2725 if (UnkOuter != NULL)
2727 FIXME("(%p) : outer != NULL?\n", ddraw);
2728 return CLASS_E_NOAGGREGATION; /* unchecked */
2733 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2734 return E_POINTER; /* unchecked */
2737 if (!(DDSD->dwFlags & DDSD_CAPS))
2739 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2740 DDSD->dwFlags |= DDSD_CAPS;
2743 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2745 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2746 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2749 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2751 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2752 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2753 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2756 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2757 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2759 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2762 return DDERR_NOEXCLUSIVEMODE;
2765 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2767 WARN("Application wanted to create back buffer primary surface\n");
2768 return DDERR_INVALIDCAPS;
2771 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2773 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2774 WARN("Application tries to put the surface in both system and video memory\n");
2776 return DDERR_INVALIDCAPS;
2779 /* Check cube maps but only if the size includes them */
2780 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2782 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2783 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2785 WARN("Cube map faces requested without cube map flag\n");
2786 return DDERR_INVALIDCAPS;
2788 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2789 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2791 WARN("Cube map without faces requested\n");
2792 return DDERR_INVALIDPARAMS;
2795 /* Quick tests confirm those can be created, but we don't do that yet */
2796 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2797 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2799 FIXME("Partial cube maps not supported yet\n");
2803 /* According to the msdn this flag is ignored by CreateSurface */
2804 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2805 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2807 /* Modify some flags */
2808 copy_to_surfacedesc2(&desc2, DDSD);
2809 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2811 /* Get the video mode from WineD3D - we will need it */
2812 hr = wined3d_device_get_display_mode(ddraw->wined3d_device, 0, &mode);
2815 ERR("Failed to read display mode from wined3d\n");
2816 switch(ddraw->orig_bpp)
2819 mode.format_id = WINED3DFMT_P8_UINT;
2823 mode.format_id = WINED3DFMT_B5G5R5X1_UNORM;
2827 mode.format_id = WINED3DFMT_B5G6R5_UNORM;
2831 mode.format_id = WINED3DFMT_B8G8R8_UNORM;
2835 mode.format_id = WINED3DFMT_B8G8R8X8_UNORM;
2838 mode.width = ddraw->orig_width;
2839 mode.height = ddraw->orig_height;
2842 /* No pixelformat given? Use the current screen format */
2843 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2845 desc2.dwFlags |= DDSD_PIXELFORMAT;
2846 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2848 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, mode.format_id);
2851 /* No Width or no Height? Use the original screen size
2853 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2854 !(desc2.dwFlags & DDSD_HEIGHT) )
2856 /* Invalid for non-render targets */
2857 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2859 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2861 return DDERR_INVALIDPARAMS;
2864 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2865 desc2.dwWidth = mode.width;
2866 desc2.dwHeight = mode.height;
2869 if (!desc2.dwWidth || !desc2.dwHeight)
2870 return DDERR_INVALIDPARAMS;
2872 /* Mipmap count fixes */
2873 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2875 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2877 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2879 /* Mipmap count is given, should not be 0 */
2880 if( desc2.u2.dwMipMapCount == 0 )
2881 return DDERR_INVALIDPARAMS;
2885 /* Undocumented feature: Create sublevels until
2886 * either the width or the height is 1
2888 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2889 desc2.dwWidth : desc2.dwHeight;
2890 desc2.u2.dwMipMapCount = 0;
2893 desc2.u2.dwMipMapCount += 1;
2900 /* Not-complex mipmap -> Mipmapcount = 1 */
2901 desc2.u2.dwMipMapCount = 1;
2903 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2905 /* There's a mipmap count in the created surface in any case */
2906 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2908 /* If no mipmap is given, the texture has only one level */
2910 /* The first surface is a front buffer, the back buffer is created afterwards */
2911 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2913 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2916 /* The root surface in a cube map is positive x */
2917 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2919 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2920 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2923 if ((desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2925 WINED3DPRESENT_PARAMETERS presentation_parameters;
2927 hr = wined3d_swapchain_get_present_parameters(ddraw->wined3d_swapchain, &presentation_parameters);
2930 ERR("Failed to get present parameters.\n");
2934 presentation_parameters.BackBufferWidth = mode.width;
2935 presentation_parameters.BackBufferHeight = mode.height;
2936 presentation_parameters.BackBufferFormat = mode.format_id;
2938 hr = wined3d_device_reset(ddraw->wined3d_device,
2939 &presentation_parameters, ddraw_reset_enum_callback);
2942 ERR("Failed to reset device.\n");
2947 /* Create the first surface */
2948 hr = ddraw_create_surface(ddraw, &desc2, &object, 0, version);
2951 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2954 object->is_complex_root = TRUE;
2958 /* Create Additional surfaces if necessary
2959 * This applies to Primary surfaces which have a back buffer count
2960 * set, but not to mipmap textures. In case of Mipmap textures,
2961 * wineD3D takes care of the creation of additional surfaces
2963 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2965 extra_surfaces = DDSD->dwBackBufferCount;
2966 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2967 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2968 desc2.dwBackBufferCount = 0;
2972 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2974 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2975 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2976 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2977 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2978 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2979 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2980 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2981 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2982 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2983 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2984 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2985 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2986 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2987 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2988 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces + 1, desc2, TRUE, version);
2989 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2990 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2993 hr |= CreateAdditionalSurfaces(ddraw, object, extra_surfaces, desc2, FALSE, version);
2996 /* This destroys and possibly created surfaces too */
2998 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
2999 else if (version == 4)
3000 IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
3002 IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
3007 if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3008 ddraw->primary = object;
3010 /* Create a WineD3DTexture if a texture was requested */
3011 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3013 ddraw->tex_root = object;
3014 ddraw_surface_create_texture(object);
3015 ddraw->tex_root = NULL;
3021 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
3022 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
3024 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3025 IDirectDrawSurfaceImpl *impl;
3028 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3029 iface, surface_desc, surface, outer_unknown);
3031 wined3d_mutex_lock();
3033 if (!(This->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3035 WARN("Cooperative level not set.\n");
3036 wined3d_mutex_unlock();
3037 return DDERR_NOCOOPERATIVELEVELSET;
3040 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3042 WARN("Application supplied invalid surface descriptor\n");
3043 wined3d_mutex_unlock();
3044 return DDERR_INVALIDPARAMS;
3047 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3049 if (TRACE_ON(ddraw))
3051 TRACE(" (%p) Requesting surface desc :\n", iface);
3052 DDRAW_dump_surface_desc(surface_desc);
3055 WARN("Application tried to create an explicit front or back buffer\n");
3056 wined3d_mutex_unlock();
3057 return DDERR_INVALIDCAPS;
3060 hr = CreateSurface(This, surface_desc, &impl, outer_unknown, 7);
3061 wined3d_mutex_unlock();
3068 *surface = &impl->IDirectDrawSurface7_iface;
3069 IDirectDraw7_AddRef(iface);
3070 impl->ifaceToRelease = (IUnknown *)iface;
3075 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3076 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3078 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3079 IDirectDrawSurfaceImpl *impl;
3082 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3083 iface, surface_desc, surface, outer_unknown);
3085 wined3d_mutex_lock();
3087 if (!(This->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3089 WARN("Cooperative level not set.\n");
3090 wined3d_mutex_unlock();
3091 return DDERR_NOCOOPERATIVELEVELSET;
3094 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3096 WARN("Application supplied invalid surface descriptor\n");
3097 wined3d_mutex_unlock();
3098 return DDERR_INVALIDPARAMS;
3101 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3103 if (TRACE_ON(ddraw))
3105 TRACE(" (%p) Requesting surface desc :\n", iface);
3106 DDRAW_dump_surface_desc(surface_desc);
3109 WARN("Application tried to create an explicit front or back buffer\n");
3110 wined3d_mutex_unlock();
3111 return DDERR_INVALIDCAPS;
3114 hr = CreateSurface(This, surface_desc, &impl, outer_unknown, 4);
3115 wined3d_mutex_unlock();
3122 *surface = &impl->IDirectDrawSurface4_iface;
3123 IDirectDraw4_AddRef(iface);
3124 impl->ifaceToRelease = (IUnknown *)iface;
3129 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3130 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3132 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3133 IDirectDrawSurfaceImpl *impl;
3135 DDSURFACEDESC2 surface_desc2;
3137 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3138 iface, surface_desc, surface, outer_unknown);
3140 wined3d_mutex_lock();
3142 if (!(This->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3144 WARN("Cooperative level not set.\n");
3145 wined3d_mutex_unlock();
3146 return DDERR_NOCOOPERATIVELEVELSET;
3149 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3151 WARN("Application supplied invalid surface descriptor\n");
3152 wined3d_mutex_unlock();
3153 return DDERR_INVALIDPARAMS;
3156 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3157 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3159 if (TRACE_ON(ddraw))
3161 TRACE(" (%p) Requesting surface desc :\n", iface);
3162 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3165 WARN("Application tried to create an explicit front or back buffer\n");
3166 wined3d_mutex_unlock();
3167 return DDERR_INVALIDCAPS;
3170 hr = CreateSurface(This, &surface_desc2, &impl, outer_unknown, 2);
3171 wined3d_mutex_unlock();
3178 *surface = &impl->IDirectDrawSurface_iface;
3179 impl->ifaceToRelease = NULL;
3184 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3185 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3187 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3188 IDirectDrawSurfaceImpl *impl;
3190 DDSURFACEDESC2 surface_desc2;
3192 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3193 iface, surface_desc, surface, outer_unknown);
3195 wined3d_mutex_lock();
3197 if (!(This->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3199 WARN("Cooperative level not set.\n");
3200 wined3d_mutex_unlock();
3201 return DDERR_NOCOOPERATIVELEVELSET;
3204 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3206 WARN("Application supplied invalid surface descriptor\n");
3207 wined3d_mutex_unlock();
3208 return DDERR_INVALIDPARAMS;
3211 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3212 * primaries anyway. */
3213 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3214 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3215 hr = CreateSurface(This, &surface_desc2, &impl, outer_unknown, 1);
3216 wined3d_mutex_unlock();
3223 *surface = &impl->IDirectDrawSurface_iface;
3224 impl->ifaceToRelease = NULL;
3229 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3230 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3233 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3234 const DDPIXELFORMAT *provided)
3236 /* Some flags must be present in both or neither for a match. */
3237 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3238 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3239 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3241 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3244 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3247 if (requested->dwFlags & DDPF_FOURCC)
3248 if (requested->dwFourCC != provided->dwFourCC)
3251 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3252 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3253 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3256 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3257 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3258 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3261 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3262 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3265 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3266 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3268 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3271 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3272 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3278 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3287 #define CMP(FLAG, FIELD) \
3288 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3289 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3291 static const struct compare_info compare[] =
3293 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3294 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3296 CMP(CKDESTBLT, ddckCKDestBlt),
3297 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3298 CMP(CKSRCBLT, ddckCKSrcBlt),
3299 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3300 CMP(HEIGHT, dwHeight),
3301 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3302 CMP(LPSURFACE, lpSurface),
3303 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3304 CMP(PITCH, u1 /* lPitch */),
3305 /* PIXELFORMAT: manual */
3306 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3307 CMP(TEXTURESTAGE, dwTextureStage),
3308 CMP(WIDTH, dwWidth),
3309 /* ZBUFFERBITDEPTH: "obsolete" */
3316 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3319 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3321 if (requested->dwFlags & compare[i].flag
3322 && memcmp((const char *)provided + compare[i].offset,
3323 (const char *)requested + compare[i].offset,
3324 compare[i].size) != 0)
3328 if (requested->dwFlags & DDSD_PIXELFORMAT)
3330 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3331 &provided->u4.ddpfPixelFormat))
3338 #undef DDENUMSURFACES_SEARCHTYPE
3339 #undef DDENUMSURFACES_MATCHTYPE
3341 struct surfacescallback2_context
3343 LPDDENUMSURFACESCALLBACK2 func;
3347 struct surfacescallback_context
3349 LPDDENUMSURFACESCALLBACK func;
3353 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3354 DDSURFACEDESC2 *surface_desc, void *context)
3356 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3357 struct surfacescallback2_context *cbcontext = context;
3359 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3360 IDirectDrawSurface7_Release(surface);
3362 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3363 surface_desc, cbcontext->context);
3366 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3367 DDSURFACEDESC2 *surface_desc, void *context)
3369 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
3370 struct surfacescallback_context *cbcontext = context;
3372 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3373 IDirectDrawSurface7_Release(surface);
3375 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3376 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3379 /*****************************************************************************
3380 * IDirectDraw7::EnumSurfaces
3382 * Loops through all surfaces attached to this device and calls the
3383 * application callback. This can't be relayed to WineD3DDevice,
3384 * because some WineD3DSurfaces' parents are IParent objects
3387 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3388 * DDSD: Description to filter for
3389 * Context: Application-provided pointer, it's passed unmodified to the
3391 * Callback: Address to call for each surface
3394 * DDERR_INVALIDPARAMS if the callback is NULL
3397 *****************************************************************************/
3398 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3399 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3401 /* The surface enumeration is handled by WineDDraw,
3402 * because it keeps track of all surfaces attached to
3403 * it. The filtering is done by our callback function,
3404 * because WineDDraw doesn't handle ddraw-like surface
3407 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3408 IDirectDrawSurfaceImpl *surf;
3410 DDSURFACEDESC2 desc;
3411 struct list *entry, *entry2;
3413 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3414 iface, Flags, DDSD, Context, Callback);
3416 all = Flags & DDENUMSURFACES_ALL;
3417 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3420 return DDERR_INVALIDPARAMS;
3422 wined3d_mutex_lock();
3424 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3425 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3427 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3428 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3430 TRACE("Enumerating surface %p.\n", surf);
3431 desc = surf->surface_desc;
3432 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3433 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3435 wined3d_mutex_unlock();
3441 wined3d_mutex_unlock();
3446 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3447 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3449 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3450 struct surfacescallback2_context cbcontext;
3452 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3453 iface, flags, surface_desc, context, callback);
3455 cbcontext.func = callback;
3456 cbcontext.context = context;
3458 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags, surface_desc,
3459 &cbcontext, EnumSurfacesCallback2Thunk);
3462 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3463 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3465 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3466 struct surfacescallback_context cbcontext;
3467 DDSURFACEDESC2 surface_desc2;
3469 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3470 iface, flags, surface_desc, context, callback);
3472 cbcontext.func = callback;
3473 cbcontext.context = context;
3475 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3476 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags,
3477 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3480 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3481 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3483 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3484 struct surfacescallback_context cbcontext;
3485 DDSURFACEDESC2 surface_desc2;
3487 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3488 iface, flags, surface_desc, context, callback);
3490 cbcontext.func = callback;
3491 cbcontext.context = context;
3493 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3494 return ddraw7_EnumSurfaces(&This->IDirectDraw7_iface, flags,
3495 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3498 /*****************************************************************************
3499 * DirectDrawCreateClipper (DDRAW.@)
3501 * Creates a new IDirectDrawClipper object.
3504 * Clipper: Address to write the interface pointer to
3505 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3509 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3510 * E_OUTOFMEMORY if allocating the object failed
3512 *****************************************************************************/
3514 DirectDrawCreateClipper(DWORD Flags,
3515 LPDIRECTDRAWCLIPPER *Clipper,
3518 IDirectDrawClipperImpl* object;
3521 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3522 Flags, Clipper, UnkOuter);
3525 return CLASS_E_NOAGGREGATION;
3527 wined3d_mutex_lock();
3529 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3530 sizeof(IDirectDrawClipperImpl));
3533 wined3d_mutex_unlock();
3534 return E_OUTOFMEMORY;
3537 hr = ddraw_clipper_init(object);
3540 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3541 HeapFree(GetProcessHeap(), 0, object);
3542 wined3d_mutex_unlock();
3546 TRACE("Created clipper %p.\n", object);
3547 *Clipper = &object->IDirectDrawClipper_iface;
3548 wined3d_mutex_unlock();
3553 /*****************************************************************************
3554 * IDirectDraw7::CreateClipper
3556 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3558 *****************************************************************************/
3559 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3560 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3562 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3563 iface, Flags, Clipper, UnkOuter);
3565 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3568 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3569 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3571 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3573 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3574 iface, flags, clipper, outer_unknown);
3576 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3579 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3580 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3582 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3584 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3585 iface, flags, clipper, outer_unknown);
3587 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3590 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3591 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3593 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3595 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3596 iface, flags, clipper, outer_unknown);
3598 return ddraw7_CreateClipper(&This->IDirectDraw7_iface, flags, clipper, outer_unknown);
3601 /*****************************************************************************
3602 * IDirectDraw7::CreatePalette
3604 * Creates a new IDirectDrawPalette object
3607 * Flags: The flags for the new clipper
3608 * ColorTable: Color table to assign to the new clipper
3609 * Palette: Address to write the interface pointer to
3610 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3614 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3615 * E_OUTOFMEMORY if allocating the object failed
3617 *****************************************************************************/
3618 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3619 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3621 IDirectDrawImpl *This = impl_from_IDirectDraw7(iface);
3622 IDirectDrawPaletteImpl *object;
3625 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3626 iface, Flags, ColorTable, Palette, pUnkOuter);
3629 return CLASS_E_NOAGGREGATION;
3631 wined3d_mutex_lock();
3633 /* The refcount test shows that a cooplevel is required for this */
3634 if(!This->cooperative_level)
3636 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3637 wined3d_mutex_unlock();
3638 return DDERR_NOCOOPERATIVELEVELSET;
3641 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3644 ERR("Out of memory when allocating memory for a palette implementation\n");
3645 wined3d_mutex_unlock();
3646 return E_OUTOFMEMORY;
3649 hr = ddraw_palette_init(object, This, Flags, ColorTable);
3652 WARN("Failed to initialize palette, hr %#x.\n", hr);
3653 HeapFree(GetProcessHeap(), 0, object);
3654 wined3d_mutex_unlock();
3658 TRACE("Created palette %p.\n", object);
3659 *Palette = &object->IDirectDrawPalette_iface;
3660 wined3d_mutex_unlock();
3665 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3666 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3668 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3671 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3672 iface, flags, entries, palette, outer_unknown);
3674 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3675 if (SUCCEEDED(hr) && *palette)
3677 IDirectDrawPaletteImpl *impl = impl_from_IDirectDrawPalette(*palette);
3678 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3679 IDirectDraw4_AddRef(iface);
3680 impl->ifaceToRelease = (IUnknown *)iface;
3685 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3686 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3688 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3691 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3692 iface, flags, entries, palette, outer_unknown);
3694 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3695 if (SUCCEEDED(hr) && *palette)
3697 IDirectDrawPaletteImpl *impl = impl_from_IDirectDrawPalette(*palette);
3698 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3699 impl->ifaceToRelease = NULL;
3705 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3706 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3708 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3711 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3712 iface, flags, entries, palette, outer_unknown);
3714 hr = ddraw7_CreatePalette(&This->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3715 if (SUCCEEDED(hr) && *palette)
3717 IDirectDrawPaletteImpl *impl = impl_from_IDirectDrawPalette(*palette);
3718 IDirectDraw7_Release(&This->IDirectDraw7_iface);
3719 impl->ifaceToRelease = NULL;
3725 /*****************************************************************************
3726 * IDirectDraw7::DuplicateSurface
3728 * Duplicates a surface. The surface memory points to the same memory as
3729 * the original surface, and it's released when the last surface referencing
3730 * it is released. I guess that's beyond Wine's surface management right now
3731 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3732 * test application to implement this)
3735 * Src: Address of the source surface
3736 * Dest: Address to write the new surface pointer to
3739 * See IDirectDraw7::CreateSurface
3741 *****************************************************************************/
3742 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3743 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3745 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Src);
3747 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3749 /* For now, simply create a new, independent surface */
3750 return IDirectDraw7_CreateSurface(iface,
3751 &Surf->surface_desc,
3756 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3757 IDirectDrawSurface4 **dst)
3759 IDirectDrawImpl *This = impl_from_IDirectDraw4(iface);
3760 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3761 IDirectDrawSurface7 *dst7;
3762 IDirectDrawSurfaceImpl *dst_impl;
3765 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3766 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3767 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3773 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3774 *dst = &dst_impl->IDirectDrawSurface4_iface;
3775 IDirectDrawSurface4_AddRef(*dst);
3776 IDirectDrawSurface7_Release(dst7);
3781 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3782 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3784 IDirectDrawImpl *This = impl_from_IDirectDraw2(iface);
3785 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3786 IDirectDrawSurface7 *dst7;
3787 IDirectDrawSurfaceImpl *dst_impl;
3790 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3791 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3792 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3795 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3796 *dst = &dst_impl->IDirectDrawSurface_iface;
3797 IDirectDrawSurface_AddRef(*dst);
3798 IDirectDrawSurface7_Release(dst7);
3803 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3804 IDirectDrawSurface **dst)
3806 IDirectDrawImpl *This = impl_from_IDirectDraw(iface);
3807 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3808 IDirectDrawSurface7 *dst7;
3809 IDirectDrawSurfaceImpl *dst_impl;
3812 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3813 hr = ddraw7_DuplicateSurface(&This->IDirectDraw7_iface,
3814 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3817 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3818 *dst = &dst_impl->IDirectDrawSurface_iface;
3819 IDirectDrawSurface_AddRef(*dst);
3820 IDirectDrawSurface7_Release(dst7);
3825 /*****************************************************************************
3826 * IDirect3D7::EnumDevices
3828 * The EnumDevices method for IDirect3D7. It enumerates all supported
3829 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3832 * callback: Function to call for each enumerated device
3833 * context: Pointer to pass back to the app
3836 * D3D_OK, or the return value of the GetCaps call
3838 *****************************************************************************/
3839 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3841 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
3842 D3DDEVICEDESC7 device_desc7;
3843 D3DDEVICEDESC device_desc1;
3847 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3850 return DDERR_INVALIDPARAMS;
3852 wined3d_mutex_lock();
3854 hr = IDirect3DImpl_GetCaps(This->wined3d, &device_desc1, &device_desc7);
3857 wined3d_mutex_unlock();
3861 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3865 device_desc7.deviceGUID = *device_list7[i].device_guid;
3866 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3867 if (ret != DDENUMRET_OK)
3869 TRACE("Application cancelled the enumeration.\n");
3870 wined3d_mutex_unlock();
3875 TRACE("End of enumeration.\n");
3877 wined3d_mutex_unlock();
3882 /*****************************************************************************
3883 * IDirect3D3::EnumDevices
3885 * Enumerates all supported Direct3DDevice interfaces. This is the
3886 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3888 * Version 1, 2 and 3
3891 * callback: Application-provided routine to call for each enumerated device
3892 * Context: Pointer to pass to the callback
3895 * D3D_OK on success,
3896 * The result of IDirect3DImpl_GetCaps if it failed
3898 *****************************************************************************/
3899 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3901 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3903 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
3904 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3905 D3DDEVICEDESC7 device_desc7;
3908 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3909 * name string. Let's put the string in a sufficiently sized array in
3910 * writable memory. */
3911 char device_name[50];
3912 strcpy(device_name,"Direct3D HEL");
3914 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3917 return DDERR_INVALIDPARAMS;
3919 wined3d_mutex_lock();
3921 hr = IDirect3DImpl_GetCaps(This->wined3d, &device_desc1, &device_desc7);
3924 wined3d_mutex_unlock();
3928 /* Do I have to enumerate the reference id? Note from old d3d7:
3929 * "It seems that enumerating the reference IID on Direct3D 1 games
3930 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3932 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3933 * EnumReference which enables / disables enumerating the reference
3934 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3935 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3936 * demo directory suggest this.
3938 * Some games(GTA 2) seem to use the second enumerated device, so I have
3939 * to enumerate at least 2 devices. So enumerate the reference device to
3942 * Other games (Rollcage) tell emulation and hal device apart by certain
3943 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3944 * limitation flag), and it refuses all devices that have the perspective
3945 * flag set. This way it refuses the emulation device, and HAL devices
3946 * never have POW2 unset in d3d7 on windows. */
3947 if (This->d3dversion != 1)
3949 static CHAR reference_description[] = "RGB Direct3D emulation";
3951 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3952 hal_desc = device_desc1;
3953 hel_desc = device_desc1;
3954 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3955 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3956 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3957 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3958 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3959 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3960 hal_desc.dcmColorModel = 0;
3962 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3963 device_name, &hal_desc, &hel_desc, context);
3964 if (hr != D3DENUMRET_OK)
3966 TRACE("Application cancelled the enumeration.\n");
3967 wined3d_mutex_unlock();
3972 strcpy(device_name,"Direct3D HAL");
3974 TRACE("Enumerating HAL Direct3D device.\n");
3975 hal_desc = device_desc1;
3976 hel_desc = device_desc1;
3978 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3979 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3980 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3981 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3982 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3983 /* HAL devices have a HEL dcmColorModel of 0 */
3984 hel_desc.dcmColorModel = 0;
3986 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3987 device_name, &hal_desc, &hel_desc, context);
3988 if (hr != D3DENUMRET_OK)
3990 TRACE("Application cancelled the enumeration.\n");
3991 wined3d_mutex_unlock();
3995 TRACE("End of enumeration.\n");
3997 wined3d_mutex_unlock();
4002 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4004 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4006 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4008 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4011 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
4013 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4015 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
4017 return d3d3_EnumDevices(&This->IDirect3D3_iface, callback, context);
4020 /*****************************************************************************
4021 * IDirect3D3::CreateLight
4023 * Creates an IDirect3DLight interface. This interface is used in
4024 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
4025 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
4026 * uses the IDirect3DDevice7 interface with D3D7 lights.
4028 * Version 1, 2 and 3
4031 * light: Address to store the new interface pointer
4032 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4037 * DDERR_OUTOFMEMORY if memory allocation failed
4038 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4040 *****************************************************************************/
4041 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
4042 IUnknown *outer_unknown)
4044 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4045 IDirect3DLightImpl *object;
4047 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4049 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4051 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4054 ERR("Failed to allocate light memory.\n");
4055 return DDERR_OUTOFMEMORY;
4058 d3d_light_init(object, This);
4060 TRACE("Created light %p.\n", object);
4061 *light = &object->IDirect3DLight_iface;
4066 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4068 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4070 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4072 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4075 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4077 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4079 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4081 return d3d3_CreateLight(&This->IDirect3D3_iface, light, outer_unknown);
4084 /*****************************************************************************
4085 * IDirect3D3::CreateMaterial
4087 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4088 * and older versions. The IDirect3DMaterial implementation wraps its
4089 * functionality to IDirect3DDevice7::SetMaterial and friends.
4091 * Version 1, 2 and 3
4094 * material: Address to store the new interface's pointer to
4095 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4100 * DDERR_OUTOFMEMORY if memory allocation failed
4101 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4103 *****************************************************************************/
4104 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4105 IUnknown *outer_unknown)
4107 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4108 IDirect3DMaterialImpl *object;
4110 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4112 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4114 object = d3d_material_create(This);
4117 ERR("Failed to allocate material memory.\n");
4118 return DDERR_OUTOFMEMORY;
4121 TRACE("Created material %p.\n", object);
4122 *material = &object->IDirect3DMaterial3_iface;
4127 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4128 IUnknown *outer_unknown)
4130 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4131 IDirect3DMaterialImpl *object;
4133 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4135 object = d3d_material_create(This);
4138 ERR("Failed to allocate material memory.\n");
4139 return DDERR_OUTOFMEMORY;
4142 TRACE("Created material %p.\n", object);
4143 *material = &object->IDirect3DMaterial2_iface;
4148 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4149 IUnknown *outer_unknown)
4151 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4152 IDirect3DMaterialImpl *object;
4154 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4156 object = d3d_material_create(This);
4159 ERR("Failed to allocate material memory.\n");
4160 return DDERR_OUTOFMEMORY;
4163 TRACE("Created material %p.\n", object);
4164 *material = &object->IDirect3DMaterial_iface;
4169 /*****************************************************************************
4170 * IDirect3D3::CreateViewport
4172 * Creates an IDirect3DViewport interface. This interface is used
4173 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4174 * it has been replaced by a viewport structure and
4175 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4176 * uses the IDirect3DDevice7 methods for its functionality
4179 * Viewport: Address to store the new interface pointer
4180 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4185 * DDERR_OUTOFMEMORY if memory allocation failed
4186 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4188 *****************************************************************************/
4189 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4190 IUnknown *outer_unknown)
4192 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4193 IDirect3DViewportImpl *object;
4195 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4197 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4199 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4202 ERR("Failed to allocate viewport memory.\n");
4203 return DDERR_OUTOFMEMORY;
4206 d3d_viewport_init(object, This);
4208 TRACE("Created viewport %p.\n", object);
4209 *viewport = &object->IDirect3DViewport3_iface;
4214 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4216 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4218 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4220 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4224 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4226 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4228 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4230 return d3d3_CreateViewport(&This->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4234 /*****************************************************************************
4235 * IDirect3D3::FindDevice
4237 * This method finds a device with the requested properties and returns a
4238 * device description
4242 * fds: Describes the requested device characteristics
4243 * fdr: Returns the device description
4247 * DDERR_INVALIDPARAMS if no device was found
4249 *****************************************************************************/
4250 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4252 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4253 D3DDEVICEDESC7 desc7;
4254 D3DDEVICEDESC desc1;
4257 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4259 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4261 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4262 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4263 return DDERR_INVALIDPARAMS;
4265 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4266 && fds->dcmColorModel != D3DCOLOR_RGB)
4268 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4269 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4272 if (fds->dwFlags & D3DFDS_GUID)
4274 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4275 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4276 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4277 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4279 WARN("No match for this GUID.\n");
4280 return DDERR_NOTFOUND;
4285 hr = IDirect3DImpl_GetCaps(This->wined3d, &desc1, &desc7);
4286 if (hr != D3D_OK) return hr;
4288 /* Now return our own GUID */
4289 fdr->guid = IID_D3DDEVICE_WineD3D;
4290 fdr->ddHwDesc = desc1;
4291 fdr->ddSwDesc = desc1;
4293 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4298 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4300 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4302 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4304 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4307 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4309 IDirectDrawImpl *This = impl_from_IDirect3D(iface);
4311 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4313 return d3d3_FindDevice(&This->IDirect3D3_iface, fds, fdr);
4316 /*****************************************************************************
4317 * IDirect3D7::CreateDevice
4319 * Creates an IDirect3DDevice7 interface.
4321 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4322 * DirectDraw surfaces and are created with
4323 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4324 * create the device object and QueryInterfaces for IDirect3DDevice
4327 * refiid: IID of the device to create
4328 * Surface: Initial rendertarget
4329 * Device: Address to return the interface pointer
4333 * DDERR_OUTOFMEMORY if memory allocation failed
4334 * DDERR_INVALIDPARAMS if a device exists already
4336 *****************************************************************************/
4337 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4338 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4340 IDirectDrawSurfaceImpl *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4341 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4342 IDirect3DDeviceImpl *object;
4345 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4347 wined3d_mutex_lock();
4350 /* Fail device creation if non-opengl surfaces are used. */
4351 if (DefaultSurfaceType != SURFACE_OPENGL)
4353 ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry.\n");
4354 ERR("Please set the surface implementation to opengl or autodetection to allow 3D rendering.\n");
4356 /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
4357 * is caught in CreateSurface or QueryInterface. */
4358 wined3d_mutex_unlock();
4362 if (This->d3ddevice)
4364 FIXME("Only one Direct3D device per DirectDraw object supported.\n");
4365 wined3d_mutex_unlock();
4366 return DDERR_INVALIDPARAMS;
4369 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4372 ERR("Failed to allocate device memory.\n");
4373 wined3d_mutex_unlock();
4374 return DDERR_OUTOFMEMORY;
4377 hr = d3d_device_init(object, This, target);
4380 WARN("Failed to initialize device, hr %#x.\n", hr);
4381 HeapFree(GetProcessHeap(), 0, object);
4382 wined3d_mutex_unlock();
4386 TRACE("Created device %p.\n", object);
4387 *device = &object->IDirect3DDevice7_iface;
4389 wined3d_mutex_unlock();
4394 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4395 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4397 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4398 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4399 IDirect3DDevice7 *device7;
4400 IDirect3DDeviceImpl *device_impl;
4403 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4404 iface, debugstr_guid(riid), surface, device, outer_unknown);
4406 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4408 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4409 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL, device ? &device7 : NULL);
4412 device_impl = impl_from_IDirect3DDevice7(device7);
4413 *device = &device_impl->IDirect3DDevice3_iface;
4419 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4420 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4422 IDirectDrawImpl *This = impl_from_IDirect3D2(iface);
4423 IDirectDrawSurfaceImpl *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4424 IDirect3DDevice7 *device7;
4425 IDirect3DDeviceImpl *device_impl;
4428 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4429 iface, debugstr_guid(riid), surface, device);
4431 hr = d3d7_CreateDevice(&This->IDirect3D7_iface, riid,
4432 surface_impl ? &surface_impl->IDirectDrawSurface7_iface : NULL, device ? &device7 : NULL);
4435 device_impl = impl_from_IDirect3DDevice7(device7);
4436 *device = &device_impl->IDirect3DDevice2_iface;
4442 /*****************************************************************************
4443 * IDirect3D7::CreateVertexBuffer
4445 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4451 * desc: Requested Vertex buffer properties
4452 * vertex_buffer: Address to return the interface pointer at
4453 * flags: Some flags, should be 0
4457 * DDERR_OUTOFMEMORY if memory allocation failed
4458 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4459 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4461 *****************************************************************************/
4462 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4463 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4465 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4466 IDirect3DVertexBufferImpl *object;
4469 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4470 iface, desc, vertex_buffer, flags);
4472 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4474 hr = d3d_vertex_buffer_create(&object, This, desc);
4477 TRACE("Created vertex buffer %p.\n", object);
4478 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4481 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4486 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4487 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4489 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4490 IDirect3DVertexBufferImpl *object;
4493 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4494 iface, desc, vertex_buffer, flags, outer_unknown);
4497 return CLASS_E_NOAGGREGATION;
4498 if (!vertex_buffer || !desc)
4499 return DDERR_INVALIDPARAMS;
4501 hr = d3d_vertex_buffer_create(&object, This, desc);
4504 TRACE("Created vertex buffer %p.\n", object);
4505 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4508 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4513 /*****************************************************************************
4514 * IDirect3D7::EnumZBufferFormats
4516 * Enumerates all supported Z buffer pixel formats
4522 * callback: callback to call for each pixel format
4523 * context: Pointer to pass back to the callback
4527 * DDERR_INVALIDPARAMS if callback is NULL
4528 * For details, see IWineD3DDevice::EnumZBufferFormats
4530 *****************************************************************************/
4531 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4532 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4534 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4535 struct wined3d_display_mode mode;
4536 WINED3DDEVTYPE type;
4540 /* Order matters. Specifically, BattleZone II (full version) expects the
4541 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4542 static const enum wined3d_format_id formats[] =
4544 WINED3DFMT_S1_UINT_D15_UNORM,
4545 WINED3DFMT_D16_UNORM,
4546 WINED3DFMT_X8D24_UNORM,
4547 WINED3DFMT_S4X4_UINT_D24_UNORM,
4548 WINED3DFMT_D24_UNORM_S8_UINT,
4549 WINED3DFMT_D32_UNORM,
4552 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4553 iface, debugstr_guid(device_iid), callback, context);
4555 if (!callback) return DDERR_INVALIDPARAMS;
4557 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4558 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4559 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4561 TRACE("Asked for HAL device.\n");
4562 type = WINED3DDEVTYPE_HAL;
4564 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4565 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4567 TRACE("Asked for SW device.\n");
4568 type = WINED3DDEVTYPE_SW;
4570 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4572 TRACE("Asked for REF device.\n");
4573 type = WINED3DDEVTYPE_REF;
4575 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4577 TRACE("Asked for NULLREF device.\n");
4578 type = WINED3DDEVTYPE_NULLREF;
4582 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4583 type = WINED3DDEVTYPE_HAL;
4586 wined3d_mutex_lock();
4587 /* We need an adapter format from somewhere to please wined3d and WGL.
4588 * Use the current display mode. So far all cards offer the same depth
4589 * stencil format for all modes, but if some do not and applications do
4590 * not like that we'll have to find some workaround, like iterating over
4591 * all imaginable formats and collecting all the depth stencil formats we
4593 hr = wined3d_device_get_display_mode(This->wined3d_device, 0, &mode);
4595 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4597 hr = wined3d_check_device_format(This->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4598 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, formats[i], SURFACE_OPENGL);
4601 DDPIXELFORMAT pformat;
4603 memset(&pformat, 0, sizeof(pformat));
4604 pformat.dwSize = sizeof(pformat);
4605 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4607 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4608 hr = callback(&pformat, context);
4609 if (hr != DDENUMRET_OK)
4611 TRACE("Format enumeration cancelled by application.\n");
4612 wined3d_mutex_unlock();
4618 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4619 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4620 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4621 * newer enumerate both versions, so we do the same(bug 22434) */
4622 hr = wined3d_check_device_format(This->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4623 WINED3DUSAGE_DEPTHSTENCIL, WINED3DRTYPE_SURFACE, WINED3DFMT_X8D24_UNORM, SURFACE_OPENGL);
4626 DDPIXELFORMAT x8d24 =
4628 sizeof(x8d24), DDPF_ZBUFFER, 0,
4629 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4631 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4632 callback(&x8d24, context);
4635 TRACE("End of enumeration.\n");
4637 wined3d_mutex_unlock();
4642 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4643 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4645 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4647 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4648 iface, debugstr_guid(device_iid), callback, context);
4650 return d3d7_EnumZBufferFormats(&This->IDirect3D7_iface, device_iid, callback, context);
4653 /*****************************************************************************
4654 * IDirect3D7::EvictManagedTextures
4656 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4657 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4662 * D3D_OK, because it's a stub
4664 *****************************************************************************/
4665 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4667 IDirectDrawImpl *This = impl_from_IDirect3D7(iface);
4669 TRACE("iface %p!\n", iface);
4671 wined3d_mutex_lock();
4672 if (This->d3d_initialized)
4673 wined3d_device_evict_managed_resources(This->wined3d_device);
4674 wined3d_mutex_unlock();
4679 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4681 IDirectDrawImpl *This = impl_from_IDirect3D3(iface);
4683 TRACE("iface %p.\n", iface);
4685 return d3d7_EvictManagedTextures(&This->IDirect3D7_iface);
4688 /*****************************************************************************
4689 * IDirect3DImpl_GetCaps
4691 * This function retrieves the device caps from wined3d
4692 * and converts it into a D3D7 and D3D - D3D3 structure
4693 * This is a helper function called from various places in ddraw
4696 * wined3d: The interface to get the caps from
4697 * desc1: Old D3D <3 structure to fill (needed)
4698 * desc7: D3D7 device desc structure to fill (needed)
4701 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4703 *****************************************************************************/
4704 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4706 WINED3DCAPS wined3d_caps;
4709 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4711 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4713 wined3d_mutex_lock();
4714 hr = wined3d_get_device_caps(wined3d, 0, WINED3DDEVTYPE_HAL, &wined3d_caps);
4715 wined3d_mutex_unlock();
4718 WARN("Failed to get device caps, hr %#x.\n", hr);
4722 /* Copy the results into the d3d7 and d3d3 structures */
4723 desc7->dwDevCaps = wined3d_caps.DevCaps;
4724 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4725 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4726 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4727 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4728 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4729 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4730 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4731 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4732 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4733 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4735 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4736 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4738 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4739 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4740 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4741 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4743 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4744 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4745 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4746 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4748 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4749 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4751 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4752 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4754 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4755 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4757 /* Remove all non-d3d7 caps */
4758 desc7->dwDevCaps &= (
4759 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4760 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4761 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4762 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4763 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4764 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4765 D3DDEVCAPS_HWRASTERIZATION);
4767 desc7->dwStencilCaps &= (
4768 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4769 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4770 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4774 desc7->dwTextureOpCaps &= (
4775 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4776 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4777 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4778 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4779 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4780 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4781 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4782 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4784 desc7->dwVertexProcessingCaps &= (
4785 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4786 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4788 desc7->dpcLineCaps.dwMiscCaps &= (
4789 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4790 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4791 D3DPMISCCAPS_CULLCCW);
4793 desc7->dpcLineCaps.dwRasterCaps &= (
4794 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4795 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4796 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4797 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4798 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4799 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4800 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4801 D3DPRASTERCAPS_ZFOG);
4803 desc7->dpcLineCaps.dwZCmpCaps &= (
4804 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4805 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4806 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4808 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4809 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4810 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4811 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4812 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4813 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4815 desc7->dpcLineCaps.dwDestBlendCaps &= (
4816 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4817 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4818 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4819 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4820 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4822 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4823 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4824 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4825 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4827 desc7->dpcLineCaps.dwShadeCaps &= (
4828 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4829 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4830 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4831 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4832 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4833 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4834 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4836 desc7->dpcLineCaps.dwTextureCaps &= (
4837 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4838 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4839 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4840 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4842 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4843 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4844 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4845 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4846 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4847 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4848 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4850 desc7->dpcLineCaps.dwTextureBlendCaps &= (
4851 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
4852 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
4853 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
4855 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4856 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4857 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4859 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4861 /* DirectX7 always has the np2 flag set, no matter what the card
4862 * supports. Some old games (Rollcage) check the caps incorrectly.
4863 * If wined3d supports nonpow2 textures it also has np2 conditional
4865 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4868 /* Fill the missing members, and do some fixup */
4869 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4870 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4871 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4872 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4873 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4874 desc7->dpcLineCaps.dwStippleWidth = 32;
4875 desc7->dpcLineCaps.dwStippleHeight = 32;
4876 /* Use the same for the TriCaps */
4877 desc7->dpcTriCaps = desc7->dpcLineCaps;
4879 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4880 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4881 desc7->dwMinTextureWidth = 1;
4882 desc7->dwMinTextureHeight = 1;
4884 /* Convert DWORDs safely to WORDs */
4885 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4886 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4887 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4888 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4890 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4891 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4892 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4893 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4895 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4897 desc7->dwReserved1 = 0;
4898 desc7->dwReserved2 = 0;
4899 desc7->dwReserved3 = 0;
4900 desc7->dwReserved4 = 0;
4902 /* Fill the old structure */
4903 memset(desc1, 0, sizeof(*desc1));
4904 desc1->dwSize = sizeof(D3DDEVICEDESC);
4905 desc1->dwFlags = D3DDD_COLORMODEL
4907 | D3DDD_TRANSFORMCAPS
4909 | D3DDD_LIGHTINGCAPS
4912 | D3DDD_DEVICERENDERBITDEPTH
4913 | D3DDD_DEVICEZBUFFERBITDEPTH
4914 | D3DDD_MAXBUFFERSIZE
4915 | D3DDD_MAXVERTEXCOUNT;
4917 desc1->dcmColorModel = D3DCOLOR_RGB;
4918 desc1->dwDevCaps = desc7->dwDevCaps;
4919 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4920 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4921 desc1->bClipping = TRUE;
4922 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4923 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4924 | D3DLIGHTCAPS_PARALLELPOINT
4925 | D3DLIGHTCAPS_POINT
4926 | D3DLIGHTCAPS_SPOT;
4928 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4929 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4931 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4932 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4933 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4934 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4935 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4936 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4937 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4938 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4939 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4940 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4941 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4942 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4943 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4945 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4946 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4947 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4948 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4949 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4950 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4951 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4952 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4953 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4954 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4955 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4956 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4957 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4959 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4960 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4961 desc1->dwMaxBufferSize = 0;
4962 desc1->dwMaxVertexCount = 65536;
4963 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
4964 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4965 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
4966 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4967 desc1->dwMinStippleWidth = 1;
4968 desc1->dwMinStippleHeight = 1;
4969 desc1->dwMaxStippleWidth = 32;
4970 desc1->dwMaxStippleHeight = 32;
4971 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4972 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4973 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4974 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4975 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4976 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4977 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4978 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4979 desc1->dwStencilCaps = desc7->dwStencilCaps;
4980 desc1->dwFVFCaps = desc7->dwFVFCaps;
4981 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4982 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4983 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4988 /*****************************************************************************
4989 * IDirectDraw7 VTable
4990 *****************************************************************************/
4991 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4994 ddraw7_QueryInterface,
4999 ddraw7_CreateClipper,
5000 ddraw7_CreatePalette,
5001 ddraw7_CreateSurface,
5002 ddraw7_DuplicateSurface,
5003 ddraw7_EnumDisplayModes,
5004 ddraw7_EnumSurfaces,
5005 ddraw7_FlipToGDISurface,
5007 ddraw7_GetDisplayMode,
5008 ddraw7_GetFourCCCodes,
5009 ddraw7_GetGDISurface,
5010 ddraw7_GetMonitorFrequency,
5012 ddraw7_GetVerticalBlankStatus,
5014 ddraw7_RestoreDisplayMode,
5015 ddraw7_SetCooperativeLevel,
5016 ddraw7_SetDisplayMode,
5017 ddraw7_WaitForVerticalBlank,
5019 ddraw7_GetAvailableVidMem,
5021 ddraw7_GetSurfaceFromDC,
5023 ddraw7_RestoreAllSurfaces,
5024 ddraw7_TestCooperativeLevel,
5025 ddraw7_GetDeviceIdentifier,
5027 ddraw7_StartModeTest,
5031 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
5034 ddraw4_QueryInterface,
5039 ddraw4_CreateClipper,
5040 ddraw4_CreatePalette,
5041 ddraw4_CreateSurface,
5042 ddraw4_DuplicateSurface,
5043 ddraw4_EnumDisplayModes,
5044 ddraw4_EnumSurfaces,
5045 ddraw4_FlipToGDISurface,
5047 ddraw4_GetDisplayMode,
5048 ddraw4_GetFourCCCodes,
5049 ddraw4_GetGDISurface,
5050 ddraw4_GetMonitorFrequency,
5052 ddraw4_GetVerticalBlankStatus,
5054 ddraw4_RestoreDisplayMode,
5055 ddraw4_SetCooperativeLevel,
5056 ddraw4_SetDisplayMode,
5057 ddraw4_WaitForVerticalBlank,
5059 ddraw4_GetAvailableVidMem,
5061 ddraw4_GetSurfaceFromDC,
5063 ddraw4_RestoreAllSurfaces,
5064 ddraw4_TestCooperativeLevel,
5065 ddraw4_GetDeviceIdentifier,
5068 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5071 ddraw2_QueryInterface,
5076 ddraw2_CreateClipper,
5077 ddraw2_CreatePalette,
5078 ddraw2_CreateSurface,
5079 ddraw2_DuplicateSurface,
5080 ddraw2_EnumDisplayModes,
5081 ddraw2_EnumSurfaces,
5082 ddraw2_FlipToGDISurface,
5084 ddraw2_GetDisplayMode,
5085 ddraw2_GetFourCCCodes,
5086 ddraw2_GetGDISurface,
5087 ddraw2_GetMonitorFrequency,
5089 ddraw2_GetVerticalBlankStatus,
5091 ddraw2_RestoreDisplayMode,
5092 ddraw2_SetCooperativeLevel,
5093 ddraw2_SetDisplayMode,
5094 ddraw2_WaitForVerticalBlank,
5096 ddraw2_GetAvailableVidMem,
5099 static const struct IDirectDrawVtbl ddraw1_vtbl =
5102 ddraw1_QueryInterface,
5107 ddraw1_CreateClipper,
5108 ddraw1_CreatePalette,
5109 ddraw1_CreateSurface,
5110 ddraw1_DuplicateSurface,
5111 ddraw1_EnumDisplayModes,
5112 ddraw1_EnumSurfaces,
5113 ddraw1_FlipToGDISurface,
5115 ddraw1_GetDisplayMode,
5116 ddraw1_GetFourCCCodes,
5117 ddraw1_GetGDISurface,
5118 ddraw1_GetMonitorFrequency,
5120 ddraw1_GetVerticalBlankStatus,
5122 ddraw1_RestoreDisplayMode,
5123 ddraw1_SetCooperativeLevel,
5124 ddraw1_SetDisplayMode,
5125 ddraw1_WaitForVerticalBlank,
5128 static const struct IDirect3D7Vtbl d3d7_vtbl =
5130 /* IUnknown methods */
5131 d3d7_QueryInterface,
5134 /* IDirect3D7 methods */
5137 d3d7_CreateVertexBuffer,
5138 d3d7_EnumZBufferFormats,
5139 d3d7_EvictManagedTextures
5142 static const struct IDirect3D3Vtbl d3d3_vtbl =
5144 /* IUnknown methods */
5145 d3d3_QueryInterface,
5148 /* IDirect3D3 methods */
5151 d3d3_CreateMaterial,
5152 d3d3_CreateViewport,
5155 d3d3_CreateVertexBuffer,
5156 d3d3_EnumZBufferFormats,
5157 d3d3_EvictManagedTextures
5160 static const struct IDirect3D2Vtbl d3d2_vtbl =
5162 /* IUnknown methods */
5163 d3d2_QueryInterface,
5166 /* IDirect3D2 methods */
5169 d3d2_CreateMaterial,
5170 d3d2_CreateViewport,
5175 static const struct IDirect3DVtbl d3d1_vtbl =
5177 /* IUnknown methods */
5178 d3d1_QueryInterface,
5181 /* IDirect3D methods */
5185 d3d1_CreateMaterial,
5186 d3d1_CreateViewport,
5190 /*****************************************************************************
5193 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5194 * if none was found.
5196 * This function is in ddraw.c and the DDraw object space because D3D7
5197 * vertex buffers are created using the IDirect3D interface to the ddraw
5198 * object, so they can be valid across D3D devices(theoretically. The ddraw
5199 * object also owns the wined3d device
5203 * fvf: Fvf to find the decl for
5206 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5208 *****************************************************************************/
5209 struct wined3d_vertex_declaration *ddraw_find_decl(IDirectDrawImpl *This, DWORD fvf)
5211 struct wined3d_vertex_declaration *pDecl = NULL;
5213 int p, low, high; /* deliberately signed */
5214 struct FvfToDecl *convertedDecls = This->decls;
5216 TRACE("Searching for declaration for fvf %08x... ", fvf);
5219 high = This->numConvertedDecls - 1;
5220 while(low <= high) {
5221 p = (low + high) >> 1;
5223 if(convertedDecls[p].fvf == fvf) {
5224 TRACE("found %p\n", convertedDecls[p].decl);
5225 return convertedDecls[p].decl;
5226 } else if(convertedDecls[p].fvf < fvf) {
5232 TRACE("not found. Creating and inserting at position %d.\n", low);
5234 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5235 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5236 if (hr != S_OK) return NULL;
5238 if(This->declArraySize == This->numConvertedDecls) {
5239 int grow = max(This->declArraySize / 2, 8);
5240 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5241 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5242 if (!convertedDecls)
5244 wined3d_vertex_declaration_decref(pDecl);
5247 This->decls = convertedDecls;
5248 This->declArraySize += grow;
5251 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5252 convertedDecls[low].decl = pDecl;
5253 convertedDecls[low].fvf = fvf;
5254 This->numConvertedDecls++;
5256 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5260 static inline struct IDirectDrawImpl *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5262 return CONTAINING_RECORD(device_parent, struct IDirectDrawImpl, device_parent);
5265 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5266 struct wined3d_device *device)
5268 TRACE("device_parent %p, device %p.\n", device_parent, device);
5271 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
5272 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5273 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
5275 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5276 IDirectDrawSurfaceImpl *surf = NULL;
5278 DDSCAPS2 searchcaps = ddraw->tex_root->surface_desc.ddsCaps;
5280 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5281 "\tpool %#x, level %u, face %u, surface %p.\n",
5282 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5284 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5287 case WINED3DCUBEMAP_FACE_POSITIVE_X:
5288 TRACE("Asked for positive x\n");
5289 if (searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5291 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5293 surf = ddraw->tex_root; break;
5294 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
5295 TRACE("Asked for negative x\n");
5296 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
5297 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
5298 TRACE("Asked for positive y\n");
5299 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
5300 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
5301 TRACE("Asked for negative y\n");
5302 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
5303 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
5304 TRACE("Asked for positive z\n");
5305 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
5306 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
5307 TRACE("Asked for negative z\n");
5308 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
5309 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
5314 IDirectDrawSurface7 *attached;
5315 IDirectDrawSurface7_GetAttachedSurface(&ddraw->tex_root->IDirectDrawSurface7_iface, &searchcaps, &attached);
5316 surf = unsafe_impl_from_IDirectDrawSurface7(attached);
5317 IDirectDrawSurface7_Release(attached);
5319 if (!surf) ERR("root search surface not found\n");
5321 /* Find the wanted mipmap. There are enough mipmaps in the chain */
5324 IDirectDrawSurface7 *attached;
5325 IDirectDrawSurface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &searchcaps, &attached);
5326 if(!attached) ERR("Surface not found\n");
5327 surf = impl_from_IDirectDrawSurface7(attached);
5328 IDirectDrawSurface7_Release(attached);
5332 /* Return the surface */
5333 *surface = surf->wined3d_surface;
5334 wined3d_surface_incref(*surface);
5336 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *surface, surf);
5341 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5343 struct IDirectDrawImpl *ddraw = parent;
5344 ddraw->wined3d_frontbuffer = NULL;
5347 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5349 ddraw_frontbuffer_destroyed,
5352 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
5353 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
5354 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
5355 struct wined3d_surface **surface)
5357 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5360 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
5361 "\tmultisample_quality %u, lockable %u, surface %p.\n",
5362 device_parent, container_parent, width, height, format, multisample_type,
5363 multisample_quality, lockable, surface);
5365 if (ddraw->wined3d_frontbuffer)
5367 ERR("Frontbuffer already created.\n");
5371 hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format, lockable, FALSE, 0,
5372 WINED3DUSAGE_RENDERTARGET, WINED3DPOOL_DEFAULT, multisample_type, multisample_quality,
5373 DefaultSurfaceType, ddraw, &ddraw_frontbuffer_parent_ops, surface);
5375 ddraw->wined3d_frontbuffer = *surface;
5380 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
5381 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
5382 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
5384 ERR("DirectDraw doesn't have and shouldn't try creating implicit depth buffers.\n");
5388 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5389 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5390 WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
5392 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5393 "format %#x, pool %#x, usage %#x, volume %p.\n",
5394 device_parent, container_parent, width, height, depth,
5395 format, pool, usage, volume);
5397 ERR("Not implemented!\n");
5402 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5403 WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
5405 struct IDirectDrawImpl *ddraw = ddraw_from_device_parent(device_parent);
5408 TRACE("device_parent %p, present_parameters %p, swapchain %p.\n", device_parent, present_parameters, swapchain);
5410 if (ddraw->wined3d_swapchain)
5412 ERR("Swapchain already created.\n");
5416 hr = wined3d_swapchain_create(ddraw->wined3d_device, present_parameters,
5417 DefaultSurfaceType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5419 WARN("Failed to create swapchain, hr %#x.\n", hr);
5424 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5426 device_parent_wined3d_device_created,
5427 device_parent_create_surface,
5428 device_parent_create_rendertarget,
5429 device_parent_create_depth_stencil,
5430 device_parent_create_volume,
5431 device_parent_create_swapchain,
5434 HRESULT ddraw_init(IDirectDrawImpl *ddraw, WINED3DDEVTYPE device_type)
5439 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5440 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5441 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5442 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5443 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5444 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5445 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5446 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5447 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5448 ddraw->numIfaces = 1;
5451 /* Get the current screen settings. */
5453 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5455 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5456 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5458 ddraw->wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS,
5459 &ddraw->IDirectDraw7_iface);
5460 if (!ddraw->wined3d)
5462 WARN("Failed to create a wined3d object.\n");
5463 return E_OUTOFMEMORY;
5466 hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
5467 NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5470 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5471 wined3d_decref(ddraw->wined3d);
5475 list_init(&ddraw->surface_list);