wined3d: Just return the surface from wined3d_device_get_depth_stencil().
[wine.git] / dlls / ddraw / ddraw.c
blob1530685b431b1e4268d4a9aa3c185c36c0a55964
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 /* Device identifier. Don't relay it to WineD3D */
31 static const DDDEVICEIDENTIFIER2 deviceidentifier =
33 "display",
34 "DirectDraw HAL",
35 { { 0x00010001, 0x00010001 } },
36 0, 0, 0, 0,
37 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
38 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
42 static struct enum_device_entry
44 char interface_name[100];
45 char device_name[100];
46 const GUID *device_guid;
47 } device_list7[] =
49 /* T&L HAL device */
51 "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D",
52 "Wine D3D7 T&L HAL",
53 &IID_IDirect3DTnLHalDevice,
56 /* HAL device */
58 "WINE Direct3D7 Hardware acceleration using WineD3D",
59 "Direct3D HAL",
60 &IID_IDirect3DHALDevice,
63 /* RGB device */
65 "WINE Direct3D7 RGB Software Emulation using WineD3D",
66 "Wine D3D7 RGB",
67 &IID_IDirect3DRGBDevice,
71 static void STDMETHODCALLTYPE ddraw_null_wined3d_object_destroyed(void *parent) {}
73 const struct wined3d_parent_ops ddraw_null_wined3d_parent_ops =
75 ddraw_null_wined3d_object_destroyed,
78 static inline struct ddraw *impl_from_IDirectDraw(IDirectDraw *iface)
80 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw_iface);
83 static inline struct ddraw *impl_from_IDirectDraw2(IDirectDraw2 *iface)
85 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw2_iface);
88 static inline struct ddraw *impl_from_IDirectDraw4(IDirectDraw4 *iface)
90 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw4_iface);
93 static inline struct ddraw *impl_from_IDirectDraw7(IDirectDraw7 *iface)
95 return CONTAINING_RECORD(iface, struct ddraw, IDirectDraw7_iface);
98 static inline struct ddraw *impl_from_IDirect3D(IDirect3D *iface)
100 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D_iface);
103 static inline struct ddraw *impl_from_IDirect3D2(IDirect3D2 *iface)
105 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D2_iface);
108 static inline struct ddraw *impl_from_IDirect3D3(IDirect3D3 *iface)
110 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D3_iface);
113 static inline struct ddraw *impl_from_IDirect3D7(IDirect3D7 *iface)
115 return CONTAINING_RECORD(iface, struct ddraw, IDirect3D7_iface);
118 /*****************************************************************************
119 * IUnknown Methods
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
128 * method.
129 * The returned interface is AddRef()-ed before it's returned
131 * Used for version 1, 2, 4 and 7
133 * Params:
134 * refiid: Interface ID asked for
135 * obj: Used to return the interface pointer
137 * Returns:
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 struct ddraw *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 */
152 *obj = NULL;
154 if(!refiid)
156 wined3d_mutex_unlock();
157 return DDERR_INVALIDPARAMS;
160 /* Check DirectDraw Interfaces */
161 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
162 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
164 *obj = &This->IDirectDraw7_iface;
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");
176 *obj = NULL;
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);
191 /* Direct3D
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 != WINED3D_SURFACE_TYPE_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 */
236 else
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();
246 return S_OK;
249 static HRESULT WINAPI ddraw4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **object)
251 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
253 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
255 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
258 static HRESULT WINAPI ddraw2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **object)
260 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
262 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
264 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
267 static HRESULT WINAPI ddraw1_QueryInterface(IDirectDraw *iface, REFIID riid, void **object)
269 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
271 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
273 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
276 static HRESULT WINAPI d3d7_QueryInterface(IDirect3D7 *iface, REFIID riid, void **object)
278 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
280 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
282 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
285 static HRESULT WINAPI d3d3_QueryInterface(IDirect3D3 *iface, REFIID riid, void **object)
287 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
289 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
291 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
294 static HRESULT WINAPI d3d2_QueryInterface(IDirect3D2 *iface, REFIID riid, void **object)
296 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
298 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
300 return ddraw7_QueryInterface(&ddraw->IDirectDraw7_iface, riid, object);
303 static HRESULT WINAPI d3d1_QueryInterface(IDirect3D *iface, REFIID riid, void **object)
305 struct ddraw *ddraw = impl_from_IDirect3D(iface);
307 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
309 return ddraw7_QueryInterface(&ddraw->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 * ddraw 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 struct ddraw *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);
339 return ref;
342 static ULONG WINAPI ddraw4_AddRef(IDirectDraw4 *iface)
344 struct ddraw *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);
351 return ref;
354 static ULONG WINAPI ddraw2_AddRef(IDirectDraw2 *iface)
356 struct ddraw *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);
363 return ref;
366 static ULONG WINAPI ddraw1_AddRef(IDirectDraw *iface)
368 struct ddraw *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);
375 return ref;
378 static ULONG WINAPI d3d7_AddRef(IDirect3D7 *iface)
380 struct ddraw *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 struct ddraw *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 struct ddraw *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 struct ddraw *This = impl_from_IDirect3D(iface);
409 TRACE("iface %p.\n", iface);
411 return ddraw1_AddRef(&This->IDirectDraw_iface);
414 void ddraw_destroy_swapchain(struct ddraw *ddraw)
416 TRACE("Destroying the swapchain.\n");
418 wined3d_swapchain_decref(ddraw->wined3d_swapchain);
419 ddraw->wined3d_swapchain = NULL;
421 if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_OPENGL)
423 UINT i;
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");
436 else
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;
449 else
451 wined3d_device_uninit_gdi(ddraw->wined3d_device);
454 ddraw_set_swapchain_window(ddraw, NULL);
456 TRACE("Swapchain destroyed.\n");
459 /*****************************************************************************
460 * ddraw_destroy
462 * Destroys a ddraw object if all refcounts are 0. This is to share code
463 * between the IDirectDrawX::Release functions
465 * Params:
466 * This: DirectDraw object to destroy
468 *****************************************************************************/
469 static void ddraw_destroy(struct ddraw *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 struct ddraw *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))
510 ddraw_destroy(This);
512 return ref;
515 static ULONG WINAPI ddraw4_Release(IDirectDraw4 *iface)
517 struct ddraw *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))
523 ddraw_destroy(This);
525 return ref;
528 static ULONG WINAPI ddraw2_Release(IDirectDraw2 *iface)
530 struct ddraw *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))
536 ddraw_destroy(This);
538 return ref;
541 static ULONG WINAPI ddraw1_Release(IDirectDraw *iface)
543 struct ddraw *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))
549 ddraw_destroy(This);
551 return ref;
554 static ULONG WINAPI d3d7_Release(IDirect3D7 *iface)
556 struct ddraw *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 struct ddraw *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 struct ddraw *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 struct ddraw *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(struct ddraw *ddraw, HWND window)
596 /* FIXME: This looks wrong, exclusive mode should imply a destination
597 * window. */
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 return DD_OK;
612 static HRESULT ddraw_attach_d3d_device(struct ddraw *ddraw,
613 struct wined3d_swapchain_desc *swapchain_desc)
615 HWND window = swapchain_desc->device_window;
616 HRESULT hr;
618 TRACE("ddraw %p.\n", ddraw);
620 if (!window || window == GetDesktopWindow())
622 window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "Hidden D3D Window",
623 WS_DISABLED, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
624 NULL, NULL, NULL, NULL);
625 if (!window)
627 ERR("Failed to create window, last error %#x.\n", GetLastError());
628 return E_FAIL;
631 ShowWindow(window, SW_HIDE); /* Just to be sure */
632 WARN("No window for the Direct3DDevice, created hidden window %p.\n", window);
634 swapchain_desc->device_window = window;
636 else
638 TRACE("Using existing window %p for Direct3D rendering.\n", window);
640 ddraw->d3d_window = window;
642 /* Set this NOW, otherwise creating the depth stencil surface will cause a
643 * recursive loop until ram or emulated video memory is full. */
644 ddraw->d3d_initialized = TRUE;
645 hr = wined3d_device_init_3d(ddraw->wined3d_device, swapchain_desc);
646 if (FAILED(hr))
648 ddraw->d3d_initialized = FALSE;
649 return hr;
652 ddraw->declArraySize = 2;
653 ddraw->decls = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw->decls) * ddraw->declArraySize);
654 if (!ddraw->decls)
656 ERR("Error allocating an array for the converted vertex decls.\n");
657 ddraw->declArraySize = 0;
658 hr = wined3d_device_uninit_3d(ddraw->wined3d_device);
659 return E_OUTOFMEMORY;
662 TRACE("Successfully initialized 3D.\n");
664 return DD_OK;
667 static HRESULT ddraw_create_swapchain(struct ddraw *ddraw, HWND window, BOOL windowed)
669 struct wined3d_swapchain_desc swapchain_desc;
670 struct wined3d_display_mode mode;
671 HRESULT hr = WINED3D_OK;
673 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
675 ERR("Failed to get display mode.\n");
676 return hr;
679 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
680 swapchain_desc.backbuffer_width = mode.width;
681 swapchain_desc.backbuffer_height = mode.height;
682 swapchain_desc.backbuffer_format = mode.format_id;
683 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
684 swapchain_desc.device_window = window;
685 swapchain_desc.windowed = windowed;
687 if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_OPENGL)
688 hr = ddraw_attach_d3d_device(ddraw, &swapchain_desc);
689 else
690 hr = wined3d_device_init_gdi(ddraw->wined3d_device, &swapchain_desc);
692 if (FAILED(hr))
694 ERR("Failed to create swapchain, hr %#x.\n", hr);
695 return hr;
698 if (!(ddraw->wined3d_swapchain = wined3d_device_get_swapchain(ddraw->wined3d_device, 0)))
700 ERR("Failed to get swapchain.\n");
701 return DDERR_INVALIDPARAMS;
704 wined3d_swapchain_incref(ddraw->wined3d_swapchain);
705 ddraw_set_swapchain_window(ddraw, window);
707 return DD_OK;
710 /*****************************************************************************
711 * IDirectDraw7::SetCooperativeLevel
713 * Sets the cooperative level for the DirectDraw object, and the window
714 * assigned to it. The cooperative level determines the general behavior
715 * of the DirectDraw application
717 * Warning: This is quite tricky, as it's not really documented which
718 * cooperative levels can be combined with each other. If a game fails
719 * after this function, try to check the cooperative levels passed on
720 * Windows, and if it returns something different.
722 * If you think that this function caused the failure because it writes a
723 * fixme, be sure to run again with a +ddraw trace.
725 * What is known about cooperative levels (See the ddraw modes test):
726 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
727 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
728 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
729 * DDSCL_FULLSCREEN can be activated
730 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
732 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
733 * DDSCL_SETFOCUSWINDOW (partially),
734 * DDSCL_MULTITHREADED (work in progress)
736 * Unhandled flags, which should be implemented
737 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
738 * expect any difference to a normal window for wine)
739 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
740 * rendering (Possible test case: Half-Life)
742 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
744 * These don't seem very important for wine:
745 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
747 * Returns:
748 * DD_OK if the cooperative level was set successfully
749 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
750 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
751 * (Probably others too, have to investigate)
753 *****************************************************************************/
754 static HRESULT WINAPI ddraw7_SetCooperativeLevel(IDirectDraw7 *iface, HWND hwnd, DWORD cooplevel)
756 struct ddraw *This = impl_from_IDirectDraw7(iface);
757 struct wined3d_surface *rt = NULL, *ds = NULL;
758 struct wined3d_stateblock *stateblock;
759 BOOL restore_state = FALSE;
760 HWND window;
761 HRESULT hr;
763 TRACE("iface %p, window %p, flags %#x.\n", iface, hwnd, cooplevel);
764 DDRAW_dump_cooperativelevel(cooplevel);
766 wined3d_mutex_lock();
768 /* Get the old window */
769 window = This->dest_window;
771 /* Tests suggest that we need one of them: */
772 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
773 DDSCL_NORMAL |
774 DDSCL_EXCLUSIVE )))
776 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
777 wined3d_mutex_unlock();
778 return DDERR_INVALIDPARAMS;
781 if ((cooplevel & DDSCL_CREATEDEVICEWINDOW) && !(cooplevel & DDSCL_EXCLUSIVE))
783 WARN("DDSCL_CREATEDEVICEWINDOW requires DDSCL_EXCLUSIVE.\n");
784 wined3d_mutex_unlock();
785 return DDERR_INVALIDPARAMS;
788 /* Handle those levels first which set various hwnds */
789 if ((cooplevel & DDSCL_SETFOCUSWINDOW) && !(cooplevel & DDSCL_CREATEDEVICEWINDOW))
791 /* This isn't compatible with a lot of flags */
792 if (cooplevel & (DDSCL_MULTITHREADED
793 | DDSCL_FPUSETUP
794 | DDSCL_FPUPRESERVE
795 | DDSCL_ALLOWREBOOT
796 | DDSCL_ALLOWMODEX
797 | DDSCL_SETDEVICEWINDOW
798 | DDSCL_NORMAL
799 | DDSCL_EXCLUSIVE
800 | DDSCL_FULLSCREEN))
802 WARN("Called with incompatible flags, returning DDERR_INVALIDPARAMS.\n");
803 wined3d_mutex_unlock();
804 return DDERR_INVALIDPARAMS;
807 hr = ddraw_set_focus_window(This, hwnd);
808 wined3d_mutex_unlock();
809 return hr;
812 if (cooplevel & DDSCL_EXCLUSIVE)
814 if (!(cooplevel & DDSCL_FULLSCREEN) || !(hwnd || (cooplevel & DDSCL_CREATEDEVICEWINDOW)))
816 WARN("DDSCL_EXCLUSIVE requires DDSCL_FULLSCREEN and a window.\n");
817 wined3d_mutex_unlock();
818 return DDERR_INVALIDPARAMS;
821 if (cooplevel & DDSCL_CREATEDEVICEWINDOW)
823 HWND device_window;
825 if (!This->focuswindow && !(cooplevel & DDSCL_SETFOCUSWINDOW))
827 WARN("No focus window set.\n");
828 wined3d_mutex_unlock();
829 return DDERR_NOFOCUSWINDOW;
832 device_window = CreateWindowExA(0, DDRAW_WINDOW_CLASS_NAME, "DirectDrawDeviceWnd",
833 WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
834 NULL, NULL, NULL, NULL);
835 if (!device_window)
837 ERR("Failed to create window, last error %#x.\n", GetLastError());
838 wined3d_mutex_unlock();
839 return E_FAIL;
842 ShowWindow(device_window, SW_SHOW);
843 TRACE("Created a device window %p.\n", device_window);
845 /* Native apparently leaks the created device window if setting the
846 * focus window below fails. */
847 This->cooperative_level |= DDSCL_CREATEDEVICEWINDOW;
848 This->devicewindow = device_window;
850 if (cooplevel & DDSCL_SETFOCUSWINDOW)
852 if (!hwnd)
854 wined3d_mutex_unlock();
855 return DDERR_NOHWND;
858 if (FAILED(hr = ddraw_set_focus_window(This, hwnd)))
860 wined3d_mutex_unlock();
861 return hr;
865 hwnd = device_window;
868 else
870 if (This->cooperative_level & DDSCL_CREATEDEVICEWINDOW)
871 DestroyWindow(This->devicewindow);
872 This->devicewindow = NULL;
873 This->focuswindow = NULL;
876 if ((This->cooperative_level & DDSCL_EXCLUSIVE)
877 && (hwnd != window || !(cooplevel & DDSCL_EXCLUSIVE)))
878 wined3d_device_release_focus_window(This->wined3d_device);
880 if ((cooplevel & DDSCL_FULLSCREEN) != (This->cooperative_level & DDSCL_FULLSCREEN) || hwnd != window)
882 if (This->cooperative_level & DDSCL_FULLSCREEN)
883 wined3d_device_restore_fullscreen_window(This->wined3d_device, window);
885 if (cooplevel & DDSCL_FULLSCREEN)
887 struct wined3d_display_mode display_mode;
889 wined3d_get_adapter_display_mode(This->wined3d, WINED3DADAPTER_DEFAULT, &display_mode, NULL);
890 wined3d_device_setup_fullscreen_window(This->wined3d_device, hwnd,
891 display_mode.width, display_mode.height);
895 if ((cooplevel & DDSCL_EXCLUSIVE)
896 && (hwnd != window || !(This->cooperative_level & DDSCL_EXCLUSIVE)))
898 hr = wined3d_device_acquire_focus_window(This->wined3d_device, hwnd);
899 if (FAILED(hr))
901 ERR("Failed to acquire focus window, hr %#x.\n", hr);
902 wined3d_mutex_unlock();
903 return hr;
907 /* Don't override focus windows or private device windows */
908 if (hwnd && !This->focuswindow && !This->devicewindow && (hwnd != window))
909 This->dest_window = hwnd;
911 if (cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
912 wined3d_device_set_multithreaded(This->wined3d_device);
914 if (This->wined3d_swapchain)
916 if (DefaultSurfaceType != WINED3D_SURFACE_TYPE_GDI)
918 restore_state = TRUE;
920 if (FAILED(hr = wined3d_stateblock_create(This->wined3d_device, WINED3D_SBT_ALL, &stateblock)))
922 ERR("Failed to create stateblock, hr %#x.\n", hr);
923 wined3d_mutex_unlock();
924 return hr;
927 if (FAILED(hr = wined3d_stateblock_capture(stateblock)))
929 ERR("Failed to capture stateblock, hr %#x.\n", hr);
930 wined3d_stateblock_decref(stateblock);
931 wined3d_mutex_unlock();
932 return hr;
935 rt = wined3d_device_get_render_target(This->wined3d_device, 0);
936 if (rt == This->wined3d_frontbuffer)
937 rt = NULL;
938 else if (rt)
939 wined3d_surface_incref(rt);
941 if ((ds = wined3d_device_get_depth_stencil(This->wined3d_device)))
942 wined3d_surface_incref(ds);
945 ddraw_destroy_swapchain(This);
948 if (FAILED(hr = ddraw_create_swapchain(This, This->dest_window, !(cooplevel & DDSCL_FULLSCREEN))))
949 ERR("Failed to create swapchain, hr %#x.\n", hr);
951 if (restore_state)
953 if (ds)
955 wined3d_device_set_depth_stencil(This->wined3d_device, ds);
956 wined3d_surface_decref(ds);
959 if (rt)
961 wined3d_device_set_render_target(This->wined3d_device, 0, rt, FALSE);
962 wined3d_surface_decref(rt);
965 hr = wined3d_stateblock_apply(stateblock);
966 wined3d_stateblock_decref(stateblock);
967 if (FAILED(hr))
969 ERR("Failed to apply stateblock, hr %#x.\n", hr);
970 wined3d_mutex_unlock();
971 return hr;
975 /* Unhandled flags */
976 if(cooplevel & DDSCL_ALLOWREBOOT)
977 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
978 if(cooplevel & DDSCL_ALLOWMODEX)
979 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
980 if(cooplevel & DDSCL_FPUSETUP)
981 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
983 /* Store the cooperative_level */
984 This->cooperative_level = cooplevel;
985 TRACE("SetCooperativeLevel retuning DD_OK\n");
986 wined3d_mutex_unlock();
988 return DD_OK;
991 static HRESULT WINAPI ddraw4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
993 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
995 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
997 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
1000 static HRESULT WINAPI ddraw2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1002 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1004 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1006 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
1009 static HRESULT WINAPI ddraw1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1011 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1013 TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);
1015 return ddraw7_SetCooperativeLevel(&ddraw->IDirectDraw7_iface, window, flags);
1018 /*****************************************************************************
1020 * Helper function for SetDisplayMode and RestoreDisplayMode
1022 * Implements DirectDraw's SetDisplayMode, but ignores the value of
1023 * ForceRefreshRate, since it is already handled by
1024 * ddraw7_SetDisplayMode. RestoreDisplayMode can use this function
1025 * without worrying that ForceRefreshRate will override the refresh rate. For
1026 * argument and return value documentation, see
1027 * ddraw7_SetDisplayMode.
1029 *****************************************************************************/
1030 static HRESULT ddraw_set_display_mode(struct ddraw *ddraw, DWORD Width, DWORD Height,
1031 DWORD BPP, DWORD RefreshRate, DWORD Flags)
1033 struct wined3d_display_mode mode;
1034 enum wined3d_format_id format;
1035 HRESULT hr;
1037 TRACE("ddraw %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n", ddraw, Width,
1038 Height, BPP, RefreshRate, Flags);
1040 wined3d_mutex_lock();
1041 if( !Width || !Height )
1043 ERR("Width %u, Height %u, what to do?\n", Width, Height);
1044 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
1045 wined3d_mutex_unlock();
1046 return DD_OK;
1049 switch(BPP)
1051 case 8: format = WINED3DFMT_P8_UINT; break;
1052 case 15: format = WINED3DFMT_B5G5R5X1_UNORM; break;
1053 case 16: format = WINED3DFMT_B5G6R5_UNORM; break;
1054 case 24: format = WINED3DFMT_B8G8R8_UNORM; break;
1055 case 32: format = WINED3DFMT_B8G8R8X8_UNORM; break;
1056 default: format = WINED3DFMT_UNKNOWN; break;
1059 /* Check the exclusive mode
1060 if(!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1061 return DDERR_NOEXCLUSIVEMODE;
1062 * This is WRONG. Don't know if the SDK is completely
1063 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
1064 * is returned, but Half-Life 1.1.1.1 (Steam version)
1065 * depends on this
1068 mode.width = Width;
1069 mode.height = Height;
1070 mode.refresh_rate = RefreshRate;
1071 mode.format_id = format;
1072 mode.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
1074 /* TODO: The possible return values from msdn suggest that
1075 * the screen mode can't be changed if a surface is locked
1076 * or some drawing is in progress
1079 /* TODO: Lose the primary surface */
1080 hr = wined3d_set_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode);
1082 wined3d_mutex_unlock();
1084 switch(hr)
1086 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1087 default: return hr;
1091 /*****************************************************************************
1092 * IDirectDraw7::SetDisplayMode
1094 * Sets the display screen resolution, color depth and refresh frequency
1095 * when in fullscreen mode (in theory).
1096 * Possible return values listed in the SDK suggest that this method fails
1097 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
1098 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
1099 * It seems to be valid to pass 0 for With and Height, this has to be tested
1100 * It could mean that the current video mode should be left as-is. (But why
1101 * call it then?)
1103 * Params:
1104 * Height, Width: Screen dimension
1105 * BPP: Color depth in Bits per pixel
1106 * Refreshrate: Screen refresh rate
1107 * Flags: Other stuff
1109 * Returns
1110 * DD_OK on success
1112 *****************************************************************************/
1113 static HRESULT WINAPI ddraw7_SetDisplayMode(IDirectDraw7 *iface, DWORD Width, DWORD Height,
1114 DWORD BPP, DWORD RefreshRate, DWORD Flags)
1116 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1118 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1119 iface, Width, Height, BPP, RefreshRate, Flags);
1121 if (force_refresh_rate != 0)
1123 TRACE("ForceRefreshRate overriding passed-in refresh rate (%u Hz) to %u Hz\n",
1124 RefreshRate, force_refresh_rate);
1125 RefreshRate = force_refresh_rate;
1128 return ddraw_set_display_mode(ddraw, Width, Height, BPP, RefreshRate, Flags);
1131 static HRESULT WINAPI ddraw4_SetDisplayMode(IDirectDraw4 *iface, DWORD width, DWORD height,
1132 DWORD bpp, DWORD refresh_rate, DWORD flags)
1134 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1136 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1137 iface, width, height, bpp, refresh_rate, flags);
1139 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1142 static HRESULT WINAPI ddraw2_SetDisplayMode(IDirectDraw2 *iface,
1143 DWORD width, DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1145 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1147 TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
1148 iface, width, height, bpp, refresh_rate, flags);
1150 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, refresh_rate, flags);
1153 static HRESULT WINAPI ddraw1_SetDisplayMode(IDirectDraw *iface, DWORD width, DWORD height, DWORD bpp)
1155 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1157 TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);
1159 return ddraw7_SetDisplayMode(&ddraw->IDirectDraw7_iface, width, height, bpp, 0, 0);
1162 /*****************************************************************************
1163 * IDirectDraw7::RestoreDisplayMode
1165 * Restores the display mode to what it was at creation time. Basically.
1167 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
1168 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
1169 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
1170 * -> DD_1 is released. The screen should be left at 1024x768x32.
1171 * -> DD_2 is released. The screen should be set to 1400x1050x32
1172 * This case is unhandled right now, but Empire Earth does it this way.
1173 * (But perhaps there is something in SetCooperativeLevel to prevent this)
1175 * The msdn says that this method resets the display mode to what it was before
1176 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
1178 * Returns
1179 * DD_OK on success
1180 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
1182 *****************************************************************************/
1183 static HRESULT WINAPI ddraw7_RestoreDisplayMode(IDirectDraw7 *iface)
1185 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1187 TRACE("iface %p.\n", iface);
1189 return ddraw_set_display_mode(ddraw, ddraw->orig_width, ddraw->orig_height, ddraw->orig_bpp, 0, 0);
1192 static HRESULT WINAPI ddraw4_RestoreDisplayMode(IDirectDraw4 *iface)
1194 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1196 TRACE("iface %p.\n", iface);
1198 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1201 static HRESULT WINAPI ddraw2_RestoreDisplayMode(IDirectDraw2 *iface)
1203 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1205 TRACE("iface %p.\n", iface);
1207 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1210 static HRESULT WINAPI ddraw1_RestoreDisplayMode(IDirectDraw *iface)
1212 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1214 TRACE("iface %p.\n", iface);
1216 return ddraw7_RestoreDisplayMode(&ddraw->IDirectDraw7_iface);
1219 /*****************************************************************************
1220 * IDirectDraw7::GetCaps
1222 * Returns the drives capabilities
1224 * Used for version 1, 2, 4 and 7
1226 * Params:
1227 * DriverCaps: Structure to write the Hardware accelerated caps to
1228 * HelCaps: Structure to write the emulation caps to
1230 * Returns
1231 * This implementation returns DD_OK only
1233 *****************************************************************************/
1234 static HRESULT WINAPI ddraw7_GetCaps(IDirectDraw7 *iface, DDCAPS *DriverCaps, DDCAPS *HELCaps)
1236 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1237 DDCAPS caps;
1238 WINED3DCAPS winecaps;
1239 HRESULT hr;
1240 DDSCAPS2 ddscaps = {0, 0, 0, 0};
1242 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, DriverCaps, HELCaps);
1244 /* One structure must be != NULL */
1245 if (!DriverCaps && !HELCaps)
1247 WARN("Invalid parameters.\n");
1248 return DDERR_INVALIDPARAMS;
1251 memset(&caps, 0, sizeof(caps));
1252 memset(&winecaps, 0, sizeof(winecaps));
1253 caps.dwSize = sizeof(caps);
1255 wined3d_mutex_lock();
1256 hr = wined3d_device_get_device_caps(ddraw->wined3d_device, &winecaps);
1257 if (FAILED(hr))
1259 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
1260 wined3d_mutex_unlock();
1261 return hr;
1264 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
1265 wined3d_mutex_unlock();
1266 if(FAILED(hr)) {
1267 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
1268 return hr;
1271 caps.dwCaps = winecaps.ddraw_caps.caps;
1272 caps.dwCaps2 = winecaps.ddraw_caps.caps2;
1273 caps.dwCKeyCaps = winecaps.ddraw_caps.color_key_caps;
1274 caps.dwFXCaps = winecaps.ddraw_caps.fx_caps;
1275 caps.dwPalCaps = winecaps.ddraw_caps.pal_caps;
1276 caps.ddsCaps.dwCaps = winecaps.ddraw_caps.dds_caps;
1277 caps.dwSVBCaps = winecaps.ddraw_caps.svb_caps;
1278 caps.dwSVBCKeyCaps = winecaps.ddraw_caps.svb_color_key_caps;
1279 caps.dwSVBFXCaps = winecaps.ddraw_caps.svb_fx_caps;
1280 caps.dwVSBCaps = winecaps.ddraw_caps.vsb_caps;
1281 caps.dwVSBCKeyCaps = winecaps.ddraw_caps.vsb_color_key_caps;
1282 caps.dwVSBFXCaps = winecaps.ddraw_caps.vsb_fx_caps;
1283 caps.dwSSBCaps = winecaps.ddraw_caps.ssb_caps;
1284 caps.dwSSBCKeyCaps = winecaps.ddraw_caps.ssb_color_key_caps;
1285 caps.dwSSBFXCaps = winecaps.ddraw_caps.ssb_fx_caps;
1287 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
1288 * not to use it
1290 if (DefaultSurfaceType == WINED3D_SURFACE_TYPE_GDI)
1292 caps.dwCaps &= ~DDCAPS_3D;
1293 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
1295 if (winecaps.ddraw_caps.stride_align)
1297 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
1298 caps.dwAlignStrideAlign = winecaps.ddraw_caps.stride_align;
1301 if(DriverCaps)
1303 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
1304 if (TRACE_ON(ddraw))
1306 TRACE("Driver Caps :\n");
1307 DDRAW_dump_DDCAPS(DriverCaps);
1311 if(HELCaps)
1313 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
1314 if (TRACE_ON(ddraw))
1316 TRACE("HEL Caps :\n");
1317 DDRAW_dump_DDCAPS(HELCaps);
1321 return DD_OK;
1324 static HRESULT WINAPI ddraw4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1326 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1328 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1330 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1333 static HRESULT WINAPI ddraw2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1335 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1337 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1339 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1342 static HRESULT WINAPI ddraw1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
1344 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1346 TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);
1348 return ddraw7_GetCaps(&ddraw->IDirectDraw7_iface, driver_caps, hel_caps);
1351 /*****************************************************************************
1352 * IDirectDraw7::Compact
1354 * No idea what it does, MSDN says it's not implemented.
1356 * Returns
1357 * DD_OK, but this is unchecked
1359 *****************************************************************************/
1360 static HRESULT WINAPI ddraw7_Compact(IDirectDraw7 *iface)
1362 TRACE("iface %p.\n", iface);
1364 return DD_OK;
1367 static HRESULT WINAPI ddraw4_Compact(IDirectDraw4 *iface)
1369 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1371 TRACE("iface %p.\n", iface);
1373 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1376 static HRESULT WINAPI ddraw2_Compact(IDirectDraw2 *iface)
1378 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1380 TRACE("iface %p.\n", iface);
1382 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1385 static HRESULT WINAPI ddraw1_Compact(IDirectDraw *iface)
1387 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1389 TRACE("iface %p.\n", iface);
1391 return ddraw7_Compact(&ddraw->IDirectDraw7_iface);
1394 /*****************************************************************************
1395 * IDirectDraw7::GetDisplayMode
1397 * Returns information about the current display mode
1399 * Exists in Version 1, 2, 4 and 7
1401 * Params:
1402 * DDSD: Address of a surface description structure to write the info to
1404 * Returns
1405 * DD_OK
1407 *****************************************************************************/
1408 static HRESULT WINAPI ddraw7_GetDisplayMode(IDirectDraw7 *iface, DDSURFACEDESC2 *DDSD)
1410 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1411 struct wined3d_display_mode mode;
1412 HRESULT hr;
1413 DWORD Size;
1415 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1417 wined3d_mutex_lock();
1418 /* This seems sane */
1419 if (!DDSD)
1421 wined3d_mutex_unlock();
1422 return DDERR_INVALIDPARAMS;
1425 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1427 ERR("Failed to get display mode, hr %#x.\n", hr);
1428 wined3d_mutex_unlock();
1429 return hr;
1432 Size = DDSD->dwSize;
1433 memset(DDSD, 0, Size);
1435 DDSD->dwSize = Size;
1436 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
1437 DDSD->dwWidth = mode.width;
1438 DDSD->dwHeight = mode.height;
1439 DDSD->u2.dwRefreshRate = 60;
1440 DDSD->ddsCaps.dwCaps = 0;
1441 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
1442 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, mode.format_id);
1443 DDSD->u1.lPitch = mode.width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
1445 if(TRACE_ON(ddraw))
1447 TRACE("Returning surface desc :\n");
1448 DDRAW_dump_surface_desc(DDSD);
1451 wined3d_mutex_unlock();
1453 return DD_OK;
1456 static HRESULT WINAPI ddraw4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *surface_desc)
1458 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1460 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1462 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, surface_desc);
1465 static HRESULT WINAPI ddraw2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *surface_desc)
1467 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1469 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1471 /* FIXME: Test sizes, properly convert surface_desc */
1472 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1475 static HRESULT WINAPI ddraw1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *surface_desc)
1477 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1479 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1481 /* FIXME: Test sizes, properly convert surface_desc */
1482 return ddraw7_GetDisplayMode(&ddraw->IDirectDraw7_iface, (DDSURFACEDESC2 *)surface_desc);
1485 /*****************************************************************************
1486 * IDirectDraw7::GetFourCCCodes
1488 * Returns an array of supported FourCC codes.
1490 * Exists in Version 1, 2, 4 and 7
1492 * Params:
1493 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
1494 * of enumerated codes
1495 * Codes: Pointer to an array of DWORDs where the supported codes are written
1496 * to
1498 * Returns
1499 * Always returns DD_OK, as it's a stub for now
1501 *****************************************************************************/
1502 static HRESULT WINAPI ddraw7_GetFourCCCodes(IDirectDraw7 *iface, DWORD *NumCodes, DWORD *Codes)
1504 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1505 static const enum wined3d_format_id formats[] =
1507 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
1508 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
1509 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
1511 struct wined3d_display_mode mode;
1512 DWORD count = 0, i, outsize;
1513 HRESULT hr;
1515 TRACE("iface %p, codes_count %p, codes %p.\n", iface, NumCodes, Codes);
1517 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
1519 ERR("Failed to get display mode, hr %#x.\n", hr);
1520 return hr;
1523 outsize = NumCodes && Codes ? *NumCodes : 0;
1525 for (i = 0; i < (sizeof(formats) / sizeof(formats[0])); ++i)
1527 hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, WINED3D_DEVICE_TYPE_HAL,
1528 mode.format_id, 0, WINED3D_RTYPE_SURFACE, formats[i], DefaultSurfaceType);
1529 if (SUCCEEDED(hr))
1531 if (count < outsize)
1532 Codes[count] = formats[i];
1533 ++count;
1536 if(NumCodes) {
1537 TRACE("Returning %u FourCC codes\n", count);
1538 *NumCodes = count;
1541 return DD_OK;
1544 static HRESULT WINAPI ddraw4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *codes_count, DWORD *codes)
1546 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1548 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1550 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1553 static HRESULT WINAPI ddraw2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *codes_count, DWORD *codes)
1555 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1557 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1559 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1562 static HRESULT WINAPI ddraw1_GetFourCCCodes(IDirectDraw *iface, DWORD *codes_count, DWORD *codes)
1564 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1566 TRACE("iface %p, codes_count %p, codes %p.\n", iface, codes_count, codes);
1568 return ddraw7_GetFourCCCodes(&ddraw->IDirectDraw7_iface, codes_count, codes);
1571 static HRESULT WINAPI ddraw7_GetMonitorFrequency(IDirectDraw7 *iface, DWORD *frequency)
1573 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1574 struct wined3d_display_mode mode;
1575 HRESULT hr;
1577 TRACE("iface %p, frequency %p.\n", iface, frequency);
1579 wined3d_mutex_lock();
1580 hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL);
1581 wined3d_mutex_unlock();
1582 if (FAILED(hr))
1584 WARN("Failed to get display mode, hr %#x.\n", hr);
1585 return hr;
1588 *frequency = mode.refresh_rate;
1590 return DD_OK;
1593 static HRESULT WINAPI ddraw4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
1595 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1597 TRACE("iface %p, frequency %p.\n", iface, frequency);
1599 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1602 static HRESULT WINAPI ddraw2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
1604 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1606 TRACE("iface %p, frequency %p.\n", iface, frequency);
1608 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1611 static HRESULT WINAPI ddraw1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
1613 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1615 TRACE("iface %p, frequency %p.\n", iface, frequency);
1617 return ddraw7_GetMonitorFrequency(&ddraw->IDirectDraw7_iface, frequency);
1620 static HRESULT WINAPI ddraw7_GetVerticalBlankStatus(IDirectDraw7 *iface, BOOL *status)
1622 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1623 struct wined3d_raster_status raster_status;
1624 HRESULT hr;
1626 TRACE("iface %p, status %p.\n", iface, status);
1628 if(!status)
1629 return DDERR_INVALIDPARAMS;
1631 wined3d_mutex_lock();
1632 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1633 wined3d_mutex_unlock();
1634 if (FAILED(hr))
1636 WARN("Failed to get raster status, hr %#x.\n", hr);
1637 return hr;
1640 *status = raster_status.in_vblank;
1642 return DD_OK;
1645 static HRESULT WINAPI ddraw4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
1647 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1649 TRACE("iface %p, status %p.\n", iface, status);
1651 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1654 static HRESULT WINAPI ddraw2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
1656 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1658 TRACE("iface %p, status %p.\n", iface, status);
1660 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1663 static HRESULT WINAPI ddraw1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
1665 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1667 TRACE("iface %p, status %p.\n", iface, status);
1669 return ddraw7_GetVerticalBlankStatus(&ddraw->IDirectDraw7_iface, status);
1672 /*****************************************************************************
1673 * IDirectDraw7::GetAvailableVidMem
1675 * Returns the total and free video memory
1677 * Params:
1678 * Caps: Specifies the memory type asked for
1679 * total: Pointer to a DWORD to be filled with the total memory
1680 * free: Pointer to a DWORD to be filled with the free memory
1682 * Returns
1683 * DD_OK on success
1684 * DDERR_INVALIDPARAMS of free and total are NULL
1686 *****************************************************************************/
1687 static HRESULT WINAPI ddraw7_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total,
1688 DWORD *free)
1690 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1691 HRESULT hr = DD_OK;
1693 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, Caps, total, free);
1695 if (TRACE_ON(ddraw))
1697 TRACE("Asked for memory with description: ");
1698 DDRAW_dump_DDSCAPS2(Caps);
1700 wined3d_mutex_lock();
1702 /* Todo: System memory vs local video memory vs non-local video memory
1703 * The MSDN also mentions differences between texture memory and other
1704 * resources, but that's not important
1707 if( (!total) && (!free) )
1709 wined3d_mutex_unlock();
1710 return DDERR_INVALIDPARAMS;
1713 if (free)
1714 *free = wined3d_device_get_available_texture_mem(ddraw->wined3d_device);
1715 if (total)
1717 struct wined3d_adapter_identifier desc = {0};
1719 hr = wined3d_get_adapter_identifier(ddraw->wined3d, WINED3DADAPTER_DEFAULT, 0, &desc);
1720 *total = desc.video_memory;
1723 wined3d_mutex_unlock();
1725 return hr;
1728 static HRESULT WINAPI ddraw4_GetAvailableVidMem(IDirectDraw4 *iface,
1729 DDSCAPS2 *caps, DWORD *total, DWORD *free)
1731 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1733 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1735 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, caps, total, free);
1738 static HRESULT WINAPI ddraw2_GetAvailableVidMem(IDirectDraw2 *iface,
1739 DDSCAPS *caps, DWORD *total, DWORD *free)
1741 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1742 DDSCAPS2 caps2;
1744 TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);
1746 DDRAW_Convert_DDSCAPS_1_To_2(caps, &caps2);
1747 return ddraw7_GetAvailableVidMem(&ddraw->IDirectDraw7_iface, &caps2, total, free);
1750 /*****************************************************************************
1751 * IDirectDraw7::Initialize
1753 * Initializes a DirectDraw interface.
1755 * Params:
1756 * GUID: Interface identifier. Well, don't know what this is really good
1757 * for
1759 * Returns
1760 * Returns DD_OK on the first call,
1761 * DDERR_ALREADYINITIALIZED on repeated calls
1763 *****************************************************************************/
1764 static HRESULT WINAPI ddraw7_Initialize(IDirectDraw7 *iface, GUID *guid)
1766 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1768 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1770 if (ddraw->initialized)
1771 return DDERR_ALREADYINITIALIZED;
1773 /* FIXME: To properly take the GUID into account we should call
1774 * ddraw_init() here instead of in DDRAW_Create(). */
1775 if (guid)
1776 FIXME("Ignoring guid %s.\n", debugstr_guid(guid));
1778 ddraw->initialized = TRUE;
1779 return DD_OK;
1782 static HRESULT WINAPI ddraw4_Initialize(IDirectDraw4 *iface, GUID *guid)
1784 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1786 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1788 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1791 static HRESULT WINAPI ddraw2_Initialize(IDirectDraw2 *iface, GUID *guid)
1793 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1795 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1797 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1800 static HRESULT WINAPI ddraw1_Initialize(IDirectDraw *iface, GUID *guid)
1802 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1804 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
1806 return ddraw7_Initialize(&ddraw->IDirectDraw7_iface, guid);
1809 static HRESULT WINAPI d3d1_Initialize(IDirect3D *iface, REFIID riid)
1811 TRACE("iface %p, riid %s.\n", iface, debugstr_guid(riid));
1813 return DDERR_ALREADYINITIALIZED;
1816 /*****************************************************************************
1817 * IDirectDraw7::FlipToGDISurface
1819 * "Makes the surface that the GDI writes to the primary surface"
1820 * Looks like some windows specific thing we don't have to care about.
1821 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1822 * show error boxes ;)
1823 * Well, just return DD_OK.
1825 * Returns:
1826 * Always returns DD_OK
1828 *****************************************************************************/
1829 static HRESULT WINAPI ddraw7_FlipToGDISurface(IDirectDraw7 *iface)
1831 FIXME("iface %p stub!\n", iface);
1833 return DD_OK;
1836 static HRESULT WINAPI ddraw4_FlipToGDISurface(IDirectDraw4 *iface)
1838 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1840 TRACE("iface %p.\n", iface);
1842 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1845 static HRESULT WINAPI ddraw2_FlipToGDISurface(IDirectDraw2 *iface)
1847 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1849 TRACE("iface %p.\n", iface);
1851 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1854 static HRESULT WINAPI ddraw1_FlipToGDISurface(IDirectDraw *iface)
1856 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1858 TRACE("iface %p.\n", iface);
1860 return ddraw7_FlipToGDISurface(&ddraw->IDirectDraw7_iface);
1863 /*****************************************************************************
1864 * IDirectDraw7::WaitForVerticalBlank
1866 * This method allows applications to get in sync with the vertical blank
1867 * interval.
1868 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1869 * redraw the screen, most likely because of this stub
1871 * Parameters:
1872 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1873 * or DDWAITVB_BLOCKEND
1874 * h: Not used, according to MSDN
1876 * Returns:
1877 * Always returns DD_OK
1879 *****************************************************************************/
1880 static HRESULT WINAPI ddraw7_WaitForVerticalBlank(IDirectDraw7 *iface, DWORD Flags, HANDLE event)
1882 static BOOL hide;
1884 TRACE("iface %p, flags %#x, event %p.\n", iface, Flags, event);
1886 /* This function is called often, so print the fixme only once */
1887 if(!hide)
1889 FIXME("iface %p, flags %#x, event %p stub!\n", iface, Flags, event);
1890 hide = TRUE;
1893 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1894 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1895 return DDERR_UNSUPPORTED; /* unchecked */
1897 return DD_OK;
1900 static HRESULT WINAPI ddraw4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1902 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1904 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1906 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1909 static HRESULT WINAPI ddraw2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1911 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1913 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1915 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1918 static HRESULT WINAPI ddraw1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1920 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1922 TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);
1924 return ddraw7_WaitForVerticalBlank(&ddraw->IDirectDraw7_iface, flags, event);
1927 static HRESULT WINAPI ddraw7_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1929 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
1930 struct wined3d_raster_status raster_status;
1931 HRESULT hr;
1933 TRACE("iface %p, line %p.\n", iface, Scanline);
1935 wined3d_mutex_lock();
1936 hr = wined3d_get_adapter_raster_status(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &raster_status);
1937 wined3d_mutex_unlock();
1938 if (FAILED(hr))
1940 WARN("Failed to get raster status, hr %#x.\n", hr);
1941 return hr;
1944 *Scanline = raster_status.scan_line;
1946 if (raster_status.in_vblank)
1947 return DDERR_VERTICALBLANKINPROGRESS;
1949 return DD_OK;
1952 static HRESULT WINAPI ddraw4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
1954 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
1956 TRACE("iface %p, line %p.\n", iface, line);
1958 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1961 static HRESULT WINAPI ddraw2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
1963 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
1965 TRACE("iface %p, line %p.\n", iface, line);
1967 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1970 static HRESULT WINAPI ddraw1_GetScanLine(IDirectDraw *iface, DWORD *line)
1972 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
1974 TRACE("iface %p, line %p.\n", iface, line);
1976 return ddraw7_GetScanLine(&ddraw->IDirectDraw7_iface, line);
1979 /*****************************************************************************
1980 * IDirectDraw7::TestCooperativeLevel
1982 * Informs the application about the state of the video adapter, depending
1983 * on the cooperative level
1985 * Returns:
1986 * DD_OK if the device is in a sane state
1987 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1988 * if the state is not correct(See below)
1990 *****************************************************************************/
1991 static HRESULT WINAPI ddraw7_TestCooperativeLevel(IDirectDraw7 *iface)
1993 TRACE("iface %p.\n", iface);
1995 return DD_OK;
1998 static HRESULT WINAPI ddraw4_TestCooperativeLevel(IDirectDraw4 *iface)
2000 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2002 TRACE("iface %p.\n", iface);
2004 return ddraw7_TestCooperativeLevel(&ddraw->IDirectDraw7_iface);
2007 /*****************************************************************************
2008 * IDirectDraw7::GetGDISurface
2010 * Returns the surface that GDI is treating as the primary surface.
2011 * For Wine this is the front buffer
2013 * Params:
2014 * GDISurface: Address to write the surface pointer to
2016 * Returns:
2017 * DD_OK if the surface was found
2018 * DDERR_NOTFOUND if the GDI surface wasn't found
2020 *****************************************************************************/
2021 static HRESULT WINAPI ddraw7_GetGDISurface(IDirectDraw7 *iface, IDirectDrawSurface7 **GDISurface)
2023 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2025 TRACE("iface %p, surface %p.\n", iface, GDISurface);
2027 wined3d_mutex_lock();
2029 if (!(*GDISurface = &ddraw->primary->IDirectDrawSurface7_iface))
2031 WARN("Primary not created yet.\n");
2032 wined3d_mutex_unlock();
2033 return DDERR_NOTFOUND;
2035 IDirectDrawSurface7_AddRef(*GDISurface);
2037 wined3d_mutex_unlock();
2039 return DD_OK;
2042 static HRESULT WINAPI ddraw4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **surface)
2044 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2045 struct ddraw_surface *surface_impl;
2046 IDirectDrawSurface7 *surface7;
2047 HRESULT hr;
2049 TRACE("iface %p, surface %p.\n", iface, surface);
2051 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2052 if (FAILED(hr))
2054 *surface = NULL;
2055 return hr;
2057 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2058 *surface = &surface_impl->IDirectDrawSurface4_iface;
2059 IDirectDrawSurface4_AddRef(*surface);
2060 IDirectDrawSurface7_Release(surface7);
2062 return hr;
2065 static HRESULT WINAPI ddraw2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **surface)
2067 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2068 struct ddraw_surface *surface_impl;
2069 IDirectDrawSurface7 *surface7;
2070 HRESULT hr;
2072 TRACE("iface %p, surface %p.\n", iface, surface);
2074 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2075 if (FAILED(hr))
2077 *surface = NULL;
2078 return hr;
2080 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2081 *surface = &surface_impl->IDirectDrawSurface_iface;
2082 IDirectDrawSurface_AddRef(*surface);
2083 IDirectDrawSurface7_Release(surface7);
2085 return hr;
2088 static HRESULT WINAPI ddraw1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **surface)
2090 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2091 struct ddraw_surface *surface_impl;
2092 IDirectDrawSurface7 *surface7;
2093 HRESULT hr;
2095 TRACE("iface %p, surface %p.\n", iface, surface);
2097 hr = ddraw7_GetGDISurface(&ddraw->IDirectDraw7_iface, &surface7);
2098 if (FAILED(hr))
2100 *surface = NULL;
2101 return hr;
2103 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2104 *surface = &surface_impl->IDirectDrawSurface_iface;
2105 IDirectDrawSurface_AddRef(*surface);
2106 IDirectDrawSurface7_Release(surface7);
2108 return hr;
2111 struct displaymodescallback_context
2113 LPDDENUMMODESCALLBACK func;
2114 void *context;
2117 static HRESULT CALLBACK EnumDisplayModesCallbackThunk(DDSURFACEDESC2 *surface_desc, void *context)
2119 struct displaymodescallback_context *cbcontext = context;
2120 DDSURFACEDESC desc;
2122 DDSD2_to_DDSD(surface_desc, &desc);
2123 return cbcontext->func(&desc, cbcontext->context);
2126 /*****************************************************************************
2127 * IDirectDraw7::EnumDisplayModes
2129 * Enumerates the supported Display modes. The modes can be filtered with
2130 * the DDSD parameter.
2132 * Params:
2133 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES. For old ddraw
2134 * versions (3 and older?) this is reserved and must be 0.
2135 * DDSD: Surface description to filter the modes
2136 * Context: Pointer passed back to the callback function
2137 * cb: Application-provided callback function
2139 * Returns:
2140 * DD_OK on success
2141 * DDERR_INVALIDPARAMS if the callback wasn't set
2143 *****************************************************************************/
2144 static HRESULT WINAPI ddraw7_EnumDisplayModes(IDirectDraw7 *iface, DWORD Flags,
2145 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMMODESCALLBACK2 cb)
2147 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2148 struct wined3d_display_mode *enum_modes = NULL;
2149 struct wined3d_display_mode mode;
2150 unsigned int modenum, fmt;
2151 DDSURFACEDESC2 callback_sd;
2152 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
2153 DDPIXELFORMAT pixelformat;
2155 static const enum wined3d_format_id checkFormatList[] =
2157 WINED3DFMT_B8G8R8X8_UNORM,
2158 WINED3DFMT_B5G6R5_UNORM,
2159 WINED3DFMT_P8_UINT,
2162 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2163 iface, Flags, DDSD, Context, cb);
2165 if (!cb)
2166 return DDERR_INVALIDPARAMS;
2168 wined3d_mutex_lock();
2169 if(!(Flags & DDEDM_REFRESHRATES))
2171 enum_mode_array_size = 16;
2172 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(*enum_modes) * enum_mode_array_size);
2173 if (!enum_modes)
2175 ERR("Out of memory\n");
2176 wined3d_mutex_unlock();
2177 return DDERR_OUTOFMEMORY;
2181 pixelformat.dwSize = sizeof(pixelformat);
2182 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
2184 modenum = 0;
2185 while (wined3d_enum_adapter_modes(ddraw->wined3d, WINED3DADAPTER_DEFAULT, checkFormatList[fmt],
2186 WINED3D_SCANLINE_ORDERING_UNKNOWN, modenum++, &mode) == WINED3D_OK)
2188 PixelFormat_WineD3DtoDD(&pixelformat, mode.format_id);
2189 if (DDSD)
2191 if (DDSD->dwFlags & DDSD_WIDTH && mode.width != DDSD->dwWidth)
2192 continue;
2193 if (DDSD->dwFlags & DDSD_HEIGHT && mode.height != DDSD->dwHeight)
2194 continue;
2195 if (DDSD->dwFlags & DDSD_REFRESHRATE && mode.refresh_rate != DDSD->u2.dwRefreshRate)
2196 continue;
2197 if (DDSD->dwFlags & DDSD_PIXELFORMAT
2198 && pixelformat.u1.dwRGBBitCount != DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount)
2199 continue;
2202 if(!(Flags & DDEDM_REFRESHRATES))
2204 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
2205 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
2206 * to be reduced to a single unique result in such case.
2208 BOOL found = FALSE;
2209 unsigned i;
2211 for (i = 0; i < enum_mode_count; i++)
2213 if (enum_modes[i].width == mode.width && enum_modes[i].height == mode.height
2214 && enum_modes[i].format_id == mode.format_id)
2216 found = TRUE;
2217 break;
2221 if(found) continue;
2224 memset(&callback_sd, 0, sizeof(callback_sd));
2225 callback_sd.dwSize = sizeof(callback_sd);
2226 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2228 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE;
2229 if (Flags & DDEDM_REFRESHRATES)
2230 callback_sd.u2.dwRefreshRate = mode.refresh_rate;
2232 callback_sd.dwWidth = mode.width;
2233 callback_sd.dwHeight = mode.height;
2235 callback_sd.u4.ddpfPixelFormat=pixelformat;
2237 /* Calc pitch and DWORD align like MSDN says */
2238 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.width;
2239 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
2241 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
2242 callback_sd.u2.dwRefreshRate);
2244 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
2246 TRACE("Application asked to terminate the enumeration\n");
2247 HeapFree(GetProcessHeap(), 0, enum_modes);
2248 wined3d_mutex_unlock();
2249 return DD_OK;
2252 if(!(Flags & DDEDM_REFRESHRATES))
2254 if (enum_mode_count == enum_mode_array_size)
2256 struct wined3d_display_mode *new_enum_modes;
2258 enum_mode_array_size *= 2;
2259 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes,
2260 sizeof(*new_enum_modes) * enum_mode_array_size);
2261 if (!new_enum_modes)
2263 ERR("Out of memory\n");
2264 HeapFree(GetProcessHeap(), 0, enum_modes);
2265 wined3d_mutex_unlock();
2266 return DDERR_OUTOFMEMORY;
2269 enum_modes = new_enum_modes;
2272 enum_modes[enum_mode_count++] = mode;
2277 TRACE("End of enumeration\n");
2278 HeapFree(GetProcessHeap(), 0, enum_modes);
2279 wined3d_mutex_unlock();
2281 return DD_OK;
2284 static HRESULT WINAPI ddraw4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
2285 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMMODESCALLBACK2 callback)
2287 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2289 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2290 iface, flags, surface_desc, context, callback);
2292 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags, surface_desc, context, callback);
2295 static HRESULT WINAPI ddraw2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
2296 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2298 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
2299 struct displaymodescallback_context cbcontext;
2300 DDSURFACEDESC2 surface_desc2;
2302 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2303 iface, flags, surface_desc, context, callback);
2305 cbcontext.func = callback;
2306 cbcontext.context = context;
2308 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2309 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2310 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2313 static HRESULT WINAPI ddraw1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
2314 DDSURFACEDESC *surface_desc, void *context, LPDDENUMMODESCALLBACK callback)
2316 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
2317 struct displaymodescallback_context cbcontext;
2318 DDSURFACEDESC2 surface_desc2;
2320 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
2321 iface, flags, surface_desc, context, callback);
2323 cbcontext.func = callback;
2324 cbcontext.context = context;
2326 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2327 return ddraw7_EnumDisplayModes(&ddraw->IDirectDraw7_iface, flags,
2328 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumDisplayModesCallbackThunk);
2331 /*****************************************************************************
2332 * IDirectDraw7::EvaluateMode
2334 * Used with IDirectDraw7::StartModeTest to test video modes.
2335 * EvaluateMode is used to pass or fail a mode, and continue with the next
2336 * mode
2338 * Params:
2339 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
2340 * Timeout: Returns the amount of seconds left before the mode would have
2341 * been failed automatically
2343 * Returns:
2344 * This implementation always DD_OK, because it's a stub
2346 *****************************************************************************/
2347 static HRESULT WINAPI ddraw7_EvaluateMode(IDirectDraw7 *iface, DWORD Flags, DWORD *Timeout)
2349 FIXME("iface %p, flags %#x, timeout %p stub!\n", iface, Flags, Timeout);
2351 /* When implementing this, implement it in WineD3D */
2353 return DD_OK;
2356 /*****************************************************************************
2357 * IDirectDraw7::GetDeviceIdentifier
2359 * Returns the device identifier, which gives information about the driver
2360 * Our device identifier is defined at the beginning of this file.
2362 * Params:
2363 * DDDI: Address for the returned structure
2364 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
2366 * Returns:
2367 * On success it returns DD_OK
2368 * DDERR_INVALIDPARAMS if DDDI is NULL
2370 *****************************************************************************/
2371 static HRESULT WINAPI ddraw7_GetDeviceIdentifier(IDirectDraw7 *iface,
2372 DDDEVICEIDENTIFIER2 *DDDI, DWORD Flags)
2374 TRACE("iface %p, device_identifier %p, flags %#x.\n", iface, DDDI, Flags);
2376 if(!DDDI)
2377 return DDERR_INVALIDPARAMS;
2379 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
2380 * host adapter, if there's a secondary 3D adapter. This doesn't apply
2381 * to any modern hardware, nor is it interesting for Wine, so ignore it.
2382 * Size of DDDEVICEIDENTIFIER2 may be aligned to 8 bytes and thus 4
2383 * bytes too long. So only copy the relevant part of the structure
2386 memcpy(DDDI, &deviceidentifier, FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD));
2387 return DD_OK;
2390 static HRESULT WINAPI ddraw4_GetDeviceIdentifier(IDirectDraw4 *iface,
2391 DDDEVICEIDENTIFIER *identifier, DWORD flags)
2393 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2394 DDDEVICEIDENTIFIER2 identifier2;
2395 HRESULT hr;
2397 TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);
2399 hr = ddraw7_GetDeviceIdentifier(&ddraw->IDirectDraw7_iface, &identifier2, flags);
2400 DDRAW_Convert_DDDEVICEIDENTIFIER_2_To_1(&identifier2, identifier);
2402 return hr;
2405 /*****************************************************************************
2406 * IDirectDraw7::GetSurfaceFromDC
2408 * Returns the Surface for a GDI device context handle.
2409 * Is this related to IDirectDrawSurface::GetDC ???
2411 * Params:
2412 * hdc: hdc to return the surface for
2413 * Surface: Address to write the surface pointer to
2415 * Returns:
2416 * Always returns DD_OK because it's a stub
2418 *****************************************************************************/
2419 static HRESULT WINAPI ddraw7_GetSurfaceFromDC(IDirectDraw7 *iface, HDC hdc,
2420 IDirectDrawSurface7 **Surface)
2422 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2423 struct wined3d_surface *wined3d_surface;
2424 struct ddraw_surface *surface_impl;
2425 HRESULT hr;
2427 TRACE("iface %p, dc %p, surface %p.\n", iface, hdc, Surface);
2429 if (!Surface) return E_INVALIDARG;
2431 hr = wined3d_device_get_surface_from_dc(ddraw->wined3d_device, hdc, &wined3d_surface);
2432 if (FAILED(hr))
2434 TRACE("No surface found for dc %p.\n", hdc);
2435 *Surface = NULL;
2436 return DDERR_NOTFOUND;
2439 surface_impl = wined3d_surface_get_parent(wined3d_surface);
2440 *Surface = &surface_impl->IDirectDrawSurface7_iface;
2441 IDirectDrawSurface7_AddRef(*Surface);
2442 TRACE("Returning surface %p.\n", Surface);
2443 return DD_OK;
2446 static HRESULT WINAPI ddraw4_GetSurfaceFromDC(IDirectDraw4 *iface, HDC dc,
2447 IDirectDrawSurface4 **surface)
2449 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2450 struct ddraw_surface *surface_impl;
2451 IDirectDrawSurface7 *surface7;
2452 HRESULT hr;
2454 TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
2456 if (!surface) return E_INVALIDARG;
2458 hr = ddraw7_GetSurfaceFromDC(&ddraw->IDirectDraw7_iface, dc, &surface7);
2459 if (FAILED(hr))
2461 *surface = NULL;
2462 return hr;
2464 surface_impl = impl_from_IDirectDrawSurface7(surface7);
2465 /* Tests say this is true */
2466 *surface = (IDirectDrawSurface4 *)&surface_impl->IDirectDrawSurface_iface;
2467 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
2468 IDirectDrawSurface7_Release(surface7);
2470 return hr;
2473 /*****************************************************************************
2474 * IDirectDraw7::RestoreAllSurfaces
2476 * Calls the restore method of all surfaces
2478 * Params:
2480 * Returns:
2481 * Always returns DD_OK because it's a stub
2483 *****************************************************************************/
2484 static HRESULT WINAPI ddraw7_RestoreAllSurfaces(IDirectDraw7 *iface)
2486 FIXME("iface %p stub!\n", iface);
2488 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
2489 * get their parent and call their restore method. Do not implement
2490 * it in WineD3D, as restoring a surface means re-creating the
2491 * WineD3DDSurface
2493 return DD_OK;
2496 static HRESULT WINAPI ddraw4_RestoreAllSurfaces(IDirectDraw4 *iface)
2498 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
2500 TRACE("iface %p.\n", iface);
2502 return ddraw7_RestoreAllSurfaces(&ddraw->IDirectDraw7_iface);
2505 /*****************************************************************************
2506 * IDirectDraw7::StartModeTest
2508 * Tests the specified video modes to update the system registry with
2509 * refresh rate information. StartModeTest starts the mode test,
2510 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
2511 * isn't called within 15 seconds, the mode is failed automatically
2513 * As refresh rates are handled by the X server, I don't think this
2514 * Method is important
2516 * Params:
2517 * Modes: An array of mode specifications
2518 * NumModes: The number of modes in Modes
2519 * Flags: Some flags...
2521 * Returns:
2522 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
2523 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
2524 * otherwise DD_OK
2526 *****************************************************************************/
2527 static HRESULT WINAPI ddraw7_StartModeTest(IDirectDraw7 *iface, SIZE *Modes, DWORD NumModes, DWORD Flags)
2529 FIXME("iface %p, modes %p, mode_count %u, flags %#x partial stub!\n",
2530 iface, Modes, NumModes, Flags);
2532 /* This looks sane */
2533 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
2535 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
2536 * As it is not, DDERR_TESTFINISHED is returned
2537 * (hopefully that's correct
2539 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
2540 * well, that value doesn't (yet) exist in the wine headers, so ignore it
2543 return DD_OK;
2546 /*****************************************************************************
2547 * ddraw_create_surface
2549 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
2550 * with the passed parameters.
2552 * Params:
2553 * DDSD: Description of the surface to create
2554 * Surf: Address to store the interface pointer at
2556 * Returns:
2557 * DD_OK on success
2559 *****************************************************************************/
2560 static HRESULT ddraw_create_surface(struct ddraw *ddraw, DDSURFACEDESC2 *pDDSD,
2561 struct ddraw_surface **surface, UINT level, UINT version)
2563 HRESULT hr;
2565 TRACE("ddraw %p, surface_desc %p, surface %p, level %u.\n",
2566 ddraw, pDDSD, surface, level);
2568 if (TRACE_ON(ddraw))
2570 TRACE("Requesting surface desc:\n");
2571 DDRAW_dump_surface_desc(pDDSD);
2574 if ((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && DefaultSurfaceType != WINED3D_SURFACE_TYPE_OPENGL)
2576 WARN("The application requests a 3D capable surface, but a non-OpenGL surface type was set in the registry.\n");
2577 /* Do not fail surface creation, only fail 3D device creation. */
2580 /* Create the Surface object */
2581 *surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**surface));
2582 if (!*surface)
2584 ERR("Failed to allocate surface memory.\n");
2585 return DDERR_OUTOFVIDEOMEMORY;
2588 hr = ddraw_surface_init(*surface, ddraw, pDDSD, level, version);
2589 if (FAILED(hr))
2591 WARN("Failed to initialize surface, hr %#x.\n", hr);
2592 HeapFree(GetProcessHeap(), 0, *surface);
2593 return hr;
2596 /* Increase the surface counter, and attach the surface */
2597 list_add_head(&ddraw->surface_list, &(*surface)->surface_list_entry);
2599 TRACE("Created surface %p.\n", *surface);
2601 return DD_OK;
2604 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
2606 return DD_OK;
2609 /*****************************************************************************
2610 * IDirectDraw7::CreateSurface
2612 * Creates a new IDirectDrawSurface object and returns its interface.
2614 * The surface connections with wined3d are a bit tricky. Basically it works
2615 * like this:
2617 * |------------------------| |-----------------|
2618 * | DDraw surface | | WineD3DSurface |
2619 * | | | |
2620 * | WineD3DSurface |-------------->| |
2621 * | Child |<------------->| Parent |
2622 * |------------------------| |-----------------|
2624 * The DDraw surface is the parent of the wined3d surface, and it releases
2625 * the WineD3DSurface when the ddraw surface is destroyed.
2627 * However, for all surfaces which can be in a container in WineD3D,
2628 * we have to do this. These surfaces are usually complex surfaces,
2629 * so this concerns primary surfaces with a front and a back buffer,
2630 * and textures.
2632 * |------------------------| |-----------------|
2633 * | DDraw surface | | Container |
2634 * | | | |
2635 * | Child |<------------->| Parent |
2636 * | Texture |<------------->| |
2637 * | WineD3DSurface |<----| | Levels |<--|
2638 * | Complex connection | | | | |
2639 * |------------------------| | |-----------------| |
2640 * ^ | |
2641 * | | |
2642 * | | |
2643 * | |------------------| | |-----------------| |
2644 * | | IParent | |-------->| WineD3DSurface | |
2645 * | | | | | |
2646 * | | Child |<------------->| Parent | |
2647 * | | | | Container |<--|
2648 * | |------------------| |-----------------| |
2649 * | |
2650 * | |----------------------| |
2651 * | | DDraw surface 2 | |
2652 * | | | |
2653 * |<->| Complex root Child | |
2654 * | | Texture | |
2655 * | | WineD3DSurface |<----| |
2656 * | |----------------------| | |
2657 * | | |
2658 * | |---------------------| | |-----------------| |
2659 * | | IParent | |----->| WineD3DSurface | |
2660 * | | | | | |
2661 * | | Child |<---------->| Parent | |
2662 * | |---------------------| | Container |<--|
2663 * | |-----------------| |
2664 * | |
2665 * | ---More surfaces can follow--- |
2667 * The reason is that the IWineD3DSwapchain(render target container)
2668 * and the IWineD3DTexure(Texture container) release the parents
2669 * of their surface's children, but by releasing the complex root
2670 * the surfaces which are complexly attached to it are destroyed
2671 * too. See IDirectDrawSurface::Release for a more detailed
2672 * explanation.
2674 * Params:
2675 * DDSD: Description of the surface to create
2676 * Surf: Address to store the interface pointer at
2677 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2678 * aggregation, so it has to be NULL
2680 * Returns:
2681 * DD_OK on success
2682 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2683 * DDERR_* if an error occurs
2685 *****************************************************************************/
2686 static HRESULT CreateSurface(struct ddraw *ddraw, DDSURFACEDESC2 *DDSD,
2687 struct ddraw_surface **surface, IUnknown *UnkOuter, UINT version)
2689 struct ddraw_surface *object = NULL;
2690 struct wined3d_display_mode mode;
2691 HRESULT hr;
2692 DDSURFACEDESC2 desc2;
2693 const DWORD sysvidmem = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
2695 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p.\n", ddraw, DDSD, surface, UnkOuter);
2697 /* Some checks before we start */
2698 if (TRACE_ON(ddraw))
2700 TRACE(" (%p) Requesting surface desc :\n", ddraw);
2701 DDRAW_dump_surface_desc(DDSD);
2704 if (UnkOuter != NULL)
2706 FIXME("(%p) : outer != NULL?\n", ddraw);
2707 return CLASS_E_NOAGGREGATION; /* unchecked */
2710 if (!surface)
2712 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", ddraw);
2713 return E_POINTER; /* unchecked */
2716 if (!(DDSD->dwFlags & DDSD_CAPS))
2718 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2719 DDSD->dwFlags |= DDSD_CAPS;
2722 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2724 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2725 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2728 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2730 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2731 WARN("(%p) Null surface pointer specified, ignore it!\n", ddraw);
2732 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2735 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2736 !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2738 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n",
2739 ddraw);
2740 *surface = NULL;
2741 return DDERR_NOEXCLUSIVEMODE;
2744 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_BACKBUFFER | DDSCAPS_PRIMARYSURFACE))
2746 WARN("Application wanted to create back buffer primary surface\n");
2747 return DDERR_INVALIDCAPS;
2750 if((DDSD->ddsCaps.dwCaps & sysvidmem) == sysvidmem)
2752 /* This is a special switch in ddrawex.dll, but not allowed in ddraw.dll */
2753 WARN("Application tries to put the surface in both system and video memory\n");
2754 *surface = NULL;
2755 return DDERR_INVALIDCAPS;
2758 /* Check cube maps but only if the size includes them */
2759 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2761 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2762 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2764 WARN("Cube map faces requested without cube map flag\n");
2765 return DDERR_INVALIDCAPS;
2767 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2768 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2770 WARN("Cube map without faces requested\n");
2771 return DDERR_INVALIDPARAMS;
2774 /* Quick tests confirm those can be created, but we don't do that yet */
2775 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2776 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2778 FIXME("Partial cube maps not supported yet\n");
2782 /* According to the msdn this flag is ignored by CreateSurface */
2783 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2784 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2786 /* Modify some flags */
2787 copy_to_surfacedesc2(&desc2, DDSD);
2788 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2790 /* Get the video mode from WineD3D - we will need it */
2791 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
2793 ERR("Failed to get display mode, hr %#x.\n", hr);
2795 switch (ddraw->orig_bpp)
2797 case 8:
2798 mode.format_id = WINED3DFMT_P8_UINT;
2799 break;
2801 case 15:
2802 mode.format_id = WINED3DFMT_B5G5R5X1_UNORM;
2803 break;
2805 case 16:
2806 mode.format_id = WINED3DFMT_B5G6R5_UNORM;
2807 break;
2809 case 24:
2810 mode.format_id = WINED3DFMT_B8G8R8_UNORM;
2811 break;
2813 case 32:
2814 mode.format_id = WINED3DFMT_B8G8R8X8_UNORM;
2815 break;
2817 mode.width = ddraw->orig_width;
2818 mode.height = ddraw->orig_height;
2821 /* No pixelformat given? Use the current screen format */
2822 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2824 desc2.dwFlags |= DDSD_PIXELFORMAT;
2825 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2827 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, mode.format_id);
2830 /* No Width or no Height? Use the original screen size
2832 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2833 !(desc2.dwFlags & DDSD_HEIGHT) )
2835 /* Invalid for non-render targets */
2836 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2838 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2839 *surface = NULL;
2840 return DDERR_INVALIDPARAMS;
2843 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2844 desc2.dwWidth = mode.width;
2845 desc2.dwHeight = mode.height;
2848 if (!desc2.dwWidth || !desc2.dwHeight)
2849 return DDERR_INVALIDPARAMS;
2851 /* Mipmap count fixes */
2852 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2854 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2856 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2858 /* Mipmap count is given, should not be 0 */
2859 if( desc2.u2.dwMipMapCount == 0 )
2860 return DDERR_INVALIDPARAMS;
2862 else
2864 /* Undocumented feature: Create sublevels until
2865 * either the width or the height is 1
2867 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2868 desc2.dwWidth : desc2.dwHeight;
2869 desc2.u2.dwMipMapCount = 0;
2870 while( min )
2872 desc2.u2.dwMipMapCount += 1;
2873 min >>= 1;
2877 else
2879 /* Not-complex mipmap -> Mipmapcount = 1 */
2880 desc2.u2.dwMipMapCount = 1;
2883 /* There's a mipmap count in the created surface in any case */
2884 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2886 /* If no mipmap is given, the texture has only one level */
2888 /* The first surface is a front buffer, the back buffer is created afterwards */
2889 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2891 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2894 /* The root surface in a cube map is positive x */
2895 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2897 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2898 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2901 if ((desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && (ddraw->cooperative_level & DDSCL_EXCLUSIVE))
2903 struct wined3d_swapchain_desc swapchain_desc;
2905 hr = wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
2906 if (FAILED(hr))
2908 ERR("Failed to get present parameters.\n");
2909 return hr;
2912 swapchain_desc.backbuffer_width = mode.width;
2913 swapchain_desc.backbuffer_height = mode.height;
2914 swapchain_desc.backbuffer_format = mode.format_id;
2916 hr = wined3d_device_reset(ddraw->wined3d_device,
2917 &swapchain_desc, NULL, ddraw_reset_enum_callback);
2918 if (FAILED(hr))
2920 ERR("Failed to reset device.\n");
2921 return hr;
2925 /* Create the first surface */
2926 hr = ddraw_create_surface(ddraw, &desc2, &object, 0, version);
2927 if (FAILED(hr))
2929 WARN("ddraw_create_surface failed, hr %#x.\n", hr);
2930 return hr;
2932 object->is_complex_root = TRUE;
2934 *surface = object;
2936 /* Create Additional surfaces if necessary
2937 * This applies to Primary surfaces which have a back buffer count
2938 * set, but not to mipmap textures. In case of Mipmap textures,
2939 * wineD3D takes care of the creation of additional surfaces
2941 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2943 struct ddraw_surface *last = object;
2944 UINT i;
2946 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2947 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2948 desc2.dwBackBufferCount = 0;
2950 for (i = 0; i < DDSD->dwBackBufferCount; ++i)
2952 struct ddraw_surface *object2 = NULL;
2954 if (FAILED(hr = ddraw_create_surface(ddraw, &desc2, &object2, 0, version)))
2956 if (version == 7)
2957 IDirectDrawSurface7_Release(&object->IDirectDrawSurface7_iface);
2958 else if (version == 4)
2959 IDirectDrawSurface4_Release(&object->IDirectDrawSurface4_iface);
2960 else
2961 IDirectDrawSurface_Release(&object->IDirectDrawSurface_iface);
2963 return hr;
2966 /* Add the new surface to the complex attachment array. */
2967 last->complex_array[0] = object2;
2968 last = object2;
2970 /* Remove the (possible) back buffer cap from the new surface
2971 * description, because only one surface in the flipping chain is a
2972 * back buffer, one is a front buffer, the others are just primary
2973 * surfaces. */
2974 desc2.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2978 if (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2979 ddraw->primary = object;
2981 /* Create a WineD3DTexture if a texture was requested */
2982 if (desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2983 ddraw_surface_create_texture(object);
2985 return hr;
2988 static HRESULT WINAPI ddraw7_CreateSurface(IDirectDraw7 *iface, DDSURFACEDESC2 *surface_desc,
2989 IDirectDrawSurface7 **surface, IUnknown *outer_unknown)
2991 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
2992 struct ddraw_surface *impl;
2993 HRESULT hr;
2995 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
2996 iface, surface_desc, surface, outer_unknown);
2998 wined3d_mutex_lock();
3000 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3002 WARN("Cooperative level not set.\n");
3003 wined3d_mutex_unlock();
3004 return DDERR_NOCOOPERATIVELEVELSET;
3007 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3009 WARN("Application supplied invalid surface descriptor\n");
3010 wined3d_mutex_unlock();
3011 return DDERR_INVALIDPARAMS;
3014 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3016 if (TRACE_ON(ddraw))
3018 TRACE(" (%p) Requesting surface desc :\n", iface);
3019 DDRAW_dump_surface_desc(surface_desc);
3022 WARN("Application tried to create an explicit front or back buffer\n");
3023 wined3d_mutex_unlock();
3024 return DDERR_INVALIDCAPS;
3027 hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 7);
3028 wined3d_mutex_unlock();
3029 if (FAILED(hr))
3031 *surface = NULL;
3032 return hr;
3035 *surface = &impl->IDirectDrawSurface7_iface;
3036 IDirectDraw7_AddRef(iface);
3037 impl->ifaceToRelease = (IUnknown *)iface;
3039 return hr;
3042 static HRESULT WINAPI ddraw4_CreateSurface(IDirectDraw4 *iface,
3043 DDSURFACEDESC2 *surface_desc, IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
3045 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3046 struct ddraw_surface *impl;
3047 HRESULT hr;
3049 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3050 iface, surface_desc, surface, outer_unknown);
3052 wined3d_mutex_lock();
3054 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3056 WARN("Cooperative level not set.\n");
3057 wined3d_mutex_unlock();
3058 return DDERR_NOCOOPERATIVELEVELSET;
3061 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3063 WARN("Application supplied invalid surface descriptor\n");
3064 wined3d_mutex_unlock();
3065 return DDERR_INVALIDPARAMS;
3068 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3070 if (TRACE_ON(ddraw))
3072 TRACE(" (%p) Requesting surface desc :\n", iface);
3073 DDRAW_dump_surface_desc(surface_desc);
3076 WARN("Application tried to create an explicit front or back buffer\n");
3077 wined3d_mutex_unlock();
3078 return DDERR_INVALIDCAPS;
3081 hr = CreateSurface(ddraw, surface_desc, &impl, outer_unknown, 4);
3082 wined3d_mutex_unlock();
3083 if (FAILED(hr))
3085 *surface = NULL;
3086 return hr;
3089 *surface = &impl->IDirectDrawSurface4_iface;
3090 IDirectDraw4_AddRef(iface);
3091 impl->ifaceToRelease = (IUnknown *)iface;
3093 return hr;
3096 static HRESULT WINAPI ddraw2_CreateSurface(IDirectDraw2 *iface,
3097 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3099 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3100 struct ddraw_surface *impl;
3101 HRESULT hr;
3102 DDSURFACEDESC2 surface_desc2;
3104 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3105 iface, surface_desc, surface, outer_unknown);
3107 wined3d_mutex_lock();
3109 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3111 WARN("Cooperative level not set.\n");
3112 wined3d_mutex_unlock();
3113 return DDERR_NOCOOPERATIVELEVELSET;
3116 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3118 WARN("Application supplied invalid surface descriptor\n");
3119 wined3d_mutex_unlock();
3120 return DDERR_INVALIDPARAMS;
3123 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3124 if(surface_desc->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))
3126 if (TRACE_ON(ddraw))
3128 TRACE(" (%p) Requesting surface desc :\n", iface);
3129 DDRAW_dump_surface_desc((LPDDSURFACEDESC2)surface_desc);
3132 WARN("Application tried to create an explicit front or back buffer\n");
3133 wined3d_mutex_unlock();
3134 return DDERR_INVALIDCAPS;
3137 hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 2);
3138 wined3d_mutex_unlock();
3139 if (FAILED(hr))
3141 *surface = NULL;
3142 return hr;
3145 *surface = &impl->IDirectDrawSurface_iface;
3146 impl->ifaceToRelease = NULL;
3148 return hr;
3151 static HRESULT WINAPI ddraw1_CreateSurface(IDirectDraw *iface,
3152 DDSURFACEDESC *surface_desc, IDirectDrawSurface **surface, IUnknown *outer_unknown)
3154 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3155 struct ddraw_surface *impl;
3156 HRESULT hr;
3157 DDSURFACEDESC2 surface_desc2;
3159 TRACE("iface %p, surface_desc %p, surface %p, outer_unknown %p.\n",
3160 iface, surface_desc, surface, outer_unknown);
3162 wined3d_mutex_lock();
3164 if (!(ddraw->cooperative_level & (DDSCL_NORMAL | DDSCL_EXCLUSIVE)))
3166 WARN("Cooperative level not set.\n");
3167 wined3d_mutex_unlock();
3168 return DDERR_NOCOOPERATIVELEVELSET;
3171 if(surface_desc == NULL || surface_desc->dwSize != sizeof(DDSURFACEDESC))
3173 WARN("Application supplied invalid surface descriptor\n");
3174 wined3d_mutex_unlock();
3175 return DDERR_INVALIDPARAMS;
3178 /* Remove front buffer flag, this causes failure in v7, and its added to normal
3179 * primaries anyway. */
3180 surface_desc->ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER;
3181 DDSD_to_DDSD2(surface_desc, &surface_desc2);
3182 hr = CreateSurface(ddraw, &surface_desc2, &impl, outer_unknown, 1);
3183 wined3d_mutex_unlock();
3184 if (FAILED(hr))
3186 *surface = NULL;
3187 return hr;
3190 *surface = &impl->IDirectDrawSurface_iface;
3191 impl->ifaceToRelease = NULL;
3193 return hr;
3196 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
3197 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
3199 static BOOL
3200 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
3201 const DDPIXELFORMAT *provided)
3203 /* Some flags must be present in both or neither for a match. */
3204 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
3205 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
3206 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
3208 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3209 return FALSE;
3211 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
3212 return FALSE;
3214 if (requested->dwFlags & DDPF_FOURCC)
3215 if (requested->dwFourCC != provided->dwFourCC)
3216 return FALSE;
3218 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
3219 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3220 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
3221 return FALSE;
3223 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3224 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
3225 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
3226 return FALSE;
3228 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
3229 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
3230 return FALSE;
3232 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
3233 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
3234 |DDPF_BUMPDUDV))
3235 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
3236 return FALSE;
3238 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
3239 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
3240 return FALSE;
3242 return TRUE;
3245 static BOOL ddraw_match_surface_desc(const DDSURFACEDESC2 *requested, const DDSURFACEDESC2 *provided)
3247 struct compare_info
3249 DWORD flag;
3250 ptrdiff_t offset;
3251 size_t size;
3254 #define CMP(FLAG, FIELD) \
3255 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
3256 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
3258 static const struct compare_info compare[] =
3260 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
3261 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
3262 CMP(CAPS, ddsCaps),
3263 CMP(CKDESTBLT, ddckCKDestBlt),
3264 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
3265 CMP(CKSRCBLT, ddckCKSrcBlt),
3266 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
3267 CMP(HEIGHT, dwHeight),
3268 CMP(LINEARSIZE, u1 /* dwLinearSize */),
3269 CMP(LPSURFACE, lpSurface),
3270 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
3271 CMP(PITCH, u1 /* lPitch */),
3272 /* PIXELFORMAT: manual */
3273 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
3274 CMP(TEXTURESTAGE, dwTextureStage),
3275 CMP(WIDTH, dwWidth),
3276 /* ZBUFFERBITDEPTH: "obsolete" */
3279 #undef CMP
3281 unsigned int i;
3283 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
3284 return FALSE;
3286 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
3288 if (requested->dwFlags & compare[i].flag
3289 && memcmp((const char *)provided + compare[i].offset,
3290 (const char *)requested + compare[i].offset,
3291 compare[i].size) != 0)
3292 return FALSE;
3295 if (requested->dwFlags & DDSD_PIXELFORMAT)
3297 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
3298 &provided->u4.ddpfPixelFormat))
3299 return FALSE;
3302 return TRUE;
3305 #undef DDENUMSURFACES_SEARCHTYPE
3306 #undef DDENUMSURFACES_MATCHTYPE
3308 struct surfacescallback2_context
3310 LPDDENUMSURFACESCALLBACK2 func;
3311 void *context;
3314 struct surfacescallback_context
3316 LPDDENUMSURFACESCALLBACK func;
3317 void *context;
3320 static HRESULT CALLBACK EnumSurfacesCallback2Thunk(IDirectDrawSurface7 *surface,
3321 DDSURFACEDESC2 *surface_desc, void *context)
3323 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3324 struct surfacescallback2_context *cbcontext = context;
3326 IDirectDrawSurface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3327 IDirectDrawSurface7_Release(surface);
3329 return cbcontext->func(&surface_impl->IDirectDrawSurface4_iface,
3330 surface_desc, cbcontext->context);
3333 static HRESULT CALLBACK EnumSurfacesCallbackThunk(IDirectDrawSurface7 *surface,
3334 DDSURFACEDESC2 *surface_desc, void *context)
3336 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3337 struct surfacescallback_context *cbcontext = context;
3339 IDirectDrawSurface_AddRef(&surface_impl->IDirectDrawSurface_iface);
3340 IDirectDrawSurface7_Release(surface);
3342 return cbcontext->func(&surface_impl->IDirectDrawSurface_iface,
3343 (DDSURFACEDESC *)surface_desc, cbcontext->context);
3346 /*****************************************************************************
3347 * IDirectDraw7::EnumSurfaces
3349 * Loops through all surfaces attached to this device and calls the
3350 * application callback. This can't be relayed to WineD3DDevice,
3351 * because some WineD3DSurfaces' parents are IParent objects
3353 * Params:
3354 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
3355 * DDSD: Description to filter for
3356 * Context: Application-provided pointer, it's passed unmodified to the
3357 * Callback function
3358 * Callback: Address to call for each surface
3360 * Returns:
3361 * DDERR_INVALIDPARAMS if the callback is NULL
3362 * DD_OK on success
3364 *****************************************************************************/
3365 static HRESULT WINAPI ddraw7_EnumSurfaces(IDirectDraw7 *iface, DWORD Flags,
3366 DDSURFACEDESC2 *DDSD, void *Context, LPDDENUMSURFACESCALLBACK7 Callback)
3368 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3369 struct ddraw_surface *surf;
3370 BOOL all, nomatch;
3371 DDSURFACEDESC2 desc;
3372 struct list *entry, *entry2;
3374 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3375 iface, Flags, DDSD, Context, Callback);
3377 all = Flags & DDENUMSURFACES_ALL;
3378 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3380 if (!Callback)
3381 return DDERR_INVALIDPARAMS;
3383 wined3d_mutex_lock();
3385 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3386 LIST_FOR_EACH_SAFE(entry, entry2, &ddraw->surface_list)
3388 surf = LIST_ENTRY(entry, struct ddraw_surface, surface_list_entry);
3390 if (!surf->iface_count)
3392 WARN("Not enumerating surface %p because it doesn't have any references.\n", surf);
3393 continue;
3396 if (all || (nomatch != ddraw_match_surface_desc(DDSD, &surf->surface_desc)))
3398 TRACE("Enumerating surface %p.\n", surf);
3399 desc = surf->surface_desc;
3400 IDirectDrawSurface7_AddRef(&surf->IDirectDrawSurface7_iface);
3401 if (Callback(&surf->IDirectDrawSurface7_iface, &desc, Context) != DDENUMRET_OK)
3403 wined3d_mutex_unlock();
3404 return DD_OK;
3409 wined3d_mutex_unlock();
3411 return DD_OK;
3414 static HRESULT WINAPI ddraw4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
3415 DDSURFACEDESC2 *surface_desc, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3417 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3418 struct surfacescallback2_context cbcontext;
3420 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3421 iface, flags, surface_desc, context, callback);
3423 cbcontext.func = callback;
3424 cbcontext.context = context;
3426 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags, surface_desc,
3427 &cbcontext, EnumSurfacesCallback2Thunk);
3430 static HRESULT WINAPI ddraw2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
3431 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3433 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3434 struct surfacescallback_context cbcontext;
3435 DDSURFACEDESC2 surface_desc2;
3437 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3438 iface, flags, surface_desc, context, callback);
3440 cbcontext.func = callback;
3441 cbcontext.context = context;
3443 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3444 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3445 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3448 static HRESULT WINAPI ddraw1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
3449 DDSURFACEDESC *surface_desc, void *context, LPDDENUMSURFACESCALLBACK callback)
3451 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3452 struct surfacescallback_context cbcontext;
3453 DDSURFACEDESC2 surface_desc2;
3455 TRACE("iface %p, flags %#x, surface_desc %p, context %p, callback %p.\n",
3456 iface, flags, surface_desc, context, callback);
3458 cbcontext.func = callback;
3459 cbcontext.context = context;
3461 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3462 return ddraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, flags,
3463 surface_desc ? &surface_desc2 : NULL, &cbcontext, EnumSurfacesCallbackThunk);
3466 /*****************************************************************************
3467 * DirectDrawCreateClipper (DDRAW.@)
3469 * Creates a new IDirectDrawClipper object.
3471 * Params:
3472 * Clipper: Address to write the interface pointer to
3473 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3474 * NULL
3476 * Returns:
3477 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3478 * E_OUTOFMEMORY if allocating the object failed
3480 *****************************************************************************/
3481 HRESULT WINAPI DirectDrawCreateClipper(DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3483 struct ddraw_clipper *object;
3484 HRESULT hr;
3486 TRACE("flags %#x, clipper %p, outer_unknown %p.\n",
3487 flags, clipper, outer_unknown);
3489 if (outer_unknown)
3490 return CLASS_E_NOAGGREGATION;
3492 wined3d_mutex_lock();
3494 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3495 if (!object)
3497 wined3d_mutex_unlock();
3498 return E_OUTOFMEMORY;
3501 hr = ddraw_clipper_init(object);
3502 if (FAILED(hr))
3504 WARN("Failed to initialize clipper, hr %#x.\n", hr);
3505 HeapFree(GetProcessHeap(), 0, object);
3506 wined3d_mutex_unlock();
3507 return hr;
3510 TRACE("Created clipper %p.\n", object);
3511 *clipper = &object->IDirectDrawClipper_iface;
3512 wined3d_mutex_unlock();
3514 return DD_OK;
3517 /*****************************************************************************
3518 * IDirectDraw7::CreateClipper
3520 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3522 *****************************************************************************/
3523 static HRESULT WINAPI ddraw7_CreateClipper(IDirectDraw7 *iface, DWORD Flags,
3524 IDirectDrawClipper **Clipper, IUnknown *UnkOuter)
3526 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3527 iface, Flags, Clipper, UnkOuter);
3529 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3532 static HRESULT WINAPI ddraw4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
3533 IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3535 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3537 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3538 iface, flags, clipper, outer_unknown);
3540 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3543 static HRESULT WINAPI ddraw2_CreateClipper(IDirectDraw2 *iface,
3544 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3546 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3548 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3549 iface, flags, clipper, outer_unknown);
3551 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3554 static HRESULT WINAPI ddraw1_CreateClipper(IDirectDraw *iface,
3555 DWORD flags, IDirectDrawClipper **clipper, IUnknown *outer_unknown)
3557 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3559 TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
3560 iface, flags, clipper, outer_unknown);
3562 return ddraw7_CreateClipper(&ddraw->IDirectDraw7_iface, flags, clipper, outer_unknown);
3565 /*****************************************************************************
3566 * IDirectDraw7::CreatePalette
3568 * Creates a new IDirectDrawPalette object
3570 * Params:
3571 * Flags: The flags for the new clipper
3572 * ColorTable: Color table to assign to the new clipper
3573 * Palette: Address to write the interface pointer to
3574 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3575 * NULL
3577 * Returns:
3578 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3579 * E_OUTOFMEMORY if allocating the object failed
3581 *****************************************************************************/
3582 static HRESULT WINAPI ddraw7_CreatePalette(IDirectDraw7 *iface, DWORD Flags,
3583 PALETTEENTRY *ColorTable, IDirectDrawPalette **Palette, IUnknown *pUnkOuter)
3585 struct ddraw *ddraw = impl_from_IDirectDraw7(iface);
3586 struct ddraw_palette *object;
3587 HRESULT hr;
3589 TRACE("iface %p, flags %#x, color_table %p, palette %p, outer_unknown %p.\n",
3590 iface, Flags, ColorTable, Palette, pUnkOuter);
3592 if (pUnkOuter)
3593 return CLASS_E_NOAGGREGATION;
3595 wined3d_mutex_lock();
3597 /* The refcount test shows that a cooplevel is required for this */
3598 if (!ddraw->cooperative_level)
3600 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3601 wined3d_mutex_unlock();
3602 return DDERR_NOCOOPERATIVELEVELSET;
3605 object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
3606 if (!object)
3608 ERR("Out of memory when allocating memory for a palette implementation\n");
3609 wined3d_mutex_unlock();
3610 return E_OUTOFMEMORY;
3613 hr = ddraw_palette_init(object, ddraw, Flags, ColorTable);
3614 if (FAILED(hr))
3616 WARN("Failed to initialize palette, hr %#x.\n", hr);
3617 HeapFree(GetProcessHeap(), 0, object);
3618 wined3d_mutex_unlock();
3619 return hr;
3622 TRACE("Created palette %p.\n", object);
3623 *Palette = &object->IDirectDrawPalette_iface;
3624 wined3d_mutex_unlock();
3626 return DD_OK;
3629 static HRESULT WINAPI ddraw4_CreatePalette(IDirectDraw4 *iface, DWORD flags, PALETTEENTRY *entries,
3630 IDirectDrawPalette **palette, IUnknown *outer_unknown)
3632 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3633 HRESULT hr;
3635 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3636 iface, flags, entries, palette, outer_unknown);
3638 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3639 if (SUCCEEDED(hr) && *palette)
3641 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3642 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3643 IDirectDraw4_AddRef(iface);
3644 impl->ifaceToRelease = (IUnknown *)iface;
3646 return hr;
3649 static HRESULT WINAPI ddraw2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
3650 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3652 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3653 HRESULT hr;
3655 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3656 iface, flags, entries, palette, outer_unknown);
3658 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3659 if (SUCCEEDED(hr) && *palette)
3661 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3662 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3663 impl->ifaceToRelease = NULL;
3666 return hr;
3669 static HRESULT WINAPI ddraw1_CreatePalette(IDirectDraw *iface, DWORD flags,
3670 PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
3672 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3673 HRESULT hr;
3675 TRACE("iface %p, flags %#x, entries %p, palette %p, outer_unknown %p.\n",
3676 iface, flags, entries, palette, outer_unknown);
3678 hr = ddraw7_CreatePalette(&ddraw->IDirectDraw7_iface, flags, entries, palette, outer_unknown);
3679 if (SUCCEEDED(hr) && *palette)
3681 struct ddraw_palette *impl = impl_from_IDirectDrawPalette(*palette);
3682 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
3683 impl->ifaceToRelease = NULL;
3686 return hr;
3689 /*****************************************************************************
3690 * IDirectDraw7::DuplicateSurface
3692 * Duplicates a surface. The surface memory points to the same memory as
3693 * the original surface, and it's released when the last surface referencing
3694 * it is released. I guess that's beyond Wine's surface management right now
3695 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3696 * test application to implement this)
3698 * Params:
3699 * Src: Address of the source surface
3700 * Dest: Address to write the new surface pointer to
3702 * Returns:
3703 * See IDirectDraw7::CreateSurface
3705 *****************************************************************************/
3706 static HRESULT WINAPI ddraw7_DuplicateSurface(IDirectDraw7 *iface,
3707 IDirectDrawSurface7 *Src, IDirectDrawSurface7 **Dest)
3709 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(Src);
3711 FIXME("iface %p, src %p, dst %p partial stub!\n", iface, Src, Dest);
3713 /* For now, simply create a new, independent surface */
3714 return IDirectDraw7_CreateSurface(iface, &src_surface->surface_desc, Dest, NULL);
3717 static HRESULT WINAPI ddraw4_DuplicateSurface(IDirectDraw4 *iface, IDirectDrawSurface4 *src,
3718 IDirectDrawSurface4 **dst)
3720 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
3721 struct ddraw *ddraw = impl_from_IDirectDraw4(iface);
3722 struct ddraw_surface *dst_impl;
3723 IDirectDrawSurface7 *dst7;
3724 HRESULT hr;
3726 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3728 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3729 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3730 if (FAILED(hr))
3732 *dst = NULL;
3733 return hr;
3735 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3736 *dst = &dst_impl->IDirectDrawSurface4_iface;
3737 IDirectDrawSurface4_AddRef(*dst);
3738 IDirectDrawSurface7_Release(dst7);
3740 return hr;
3743 static HRESULT WINAPI ddraw2_DuplicateSurface(IDirectDraw2 *iface,
3744 IDirectDrawSurface *src, IDirectDrawSurface **dst)
3746 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3747 struct ddraw *ddraw = impl_from_IDirectDraw2(iface);
3748 struct ddraw_surface *dst_impl;
3749 IDirectDrawSurface7 *dst7;
3750 HRESULT hr;
3752 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3754 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3755 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3756 if (FAILED(hr))
3757 return hr;
3758 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3759 *dst = &dst_impl->IDirectDrawSurface_iface;
3760 IDirectDrawSurface_AddRef(*dst);
3761 IDirectDrawSurface7_Release(dst7);
3763 return hr;
3766 static HRESULT WINAPI ddraw1_DuplicateSurface(IDirectDraw *iface, IDirectDrawSurface *src,
3767 IDirectDrawSurface **dst)
3769 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
3770 struct ddraw *ddraw = impl_from_IDirectDraw(iface);
3771 struct ddraw_surface *dst_impl;
3772 IDirectDrawSurface7 *dst7;
3773 HRESULT hr;
3775 TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
3777 hr = ddraw7_DuplicateSurface(&ddraw->IDirectDraw7_iface,
3778 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, &dst7);
3779 if (FAILED(hr))
3780 return hr;
3781 dst_impl = impl_from_IDirectDrawSurface7(dst7);
3782 *dst = &dst_impl->IDirectDrawSurface_iface;
3783 IDirectDrawSurface_AddRef(*dst);
3784 IDirectDrawSurface7_Release(dst7);
3786 return hr;
3789 /*****************************************************************************
3790 * IDirect3D7::EnumDevices
3792 * The EnumDevices method for IDirect3D7. It enumerates all supported
3793 * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
3795 * Params:
3796 * callback: Function to call for each enumerated device
3797 * context: Pointer to pass back to the app
3799 * Returns:
3800 * D3D_OK, or the return value of the GetCaps call
3802 *****************************************************************************/
3803 static HRESULT WINAPI d3d7_EnumDevices(IDirect3D7 *iface, LPD3DENUMDEVICESCALLBACK7 callback, void *context)
3805 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
3806 D3DDEVICEDESC7 device_desc7;
3807 D3DDEVICEDESC device_desc1;
3808 HRESULT hr;
3809 size_t i;
3811 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3813 if (!callback)
3814 return DDERR_INVALIDPARAMS;
3816 wined3d_mutex_lock();
3818 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3819 if (hr != D3D_OK)
3821 wined3d_mutex_unlock();
3822 return hr;
3825 for (i = 0; i < sizeof(device_list7)/sizeof(device_list7[0]); i++)
3827 HRESULT ret;
3829 device_desc7.deviceGUID = *device_list7[i].device_guid;
3830 ret = callback(device_list7[i].interface_name, device_list7[i].device_name, &device_desc7, context);
3831 if (ret != DDENUMRET_OK)
3833 TRACE("Application cancelled the enumeration.\n");
3834 wined3d_mutex_unlock();
3835 return D3D_OK;
3839 TRACE("End of enumeration.\n");
3841 wined3d_mutex_unlock();
3843 return D3D_OK;
3846 /*****************************************************************************
3847 * IDirect3D3::EnumDevices
3849 * Enumerates all supported Direct3DDevice interfaces. This is the
3850 * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
3852 * Version 1, 2 and 3
3854 * Params:
3855 * callback: Application-provided routine to call for each enumerated device
3856 * Context: Pointer to pass to the callback
3858 * Returns:
3859 * D3D_OK on success,
3860 * The result of IDirect3DImpl_GetCaps if it failed
3862 *****************************************************************************/
3863 static HRESULT WINAPI d3d3_EnumDevices(IDirect3D3 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3865 static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
3867 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
3868 D3DDEVICEDESC device_desc1, hal_desc, hel_desc;
3869 D3DDEVICEDESC7 device_desc7;
3870 HRESULT hr;
3872 /* Some games (Motoracer 2 demo) have the bad idea to modify the device
3873 * name string. Let's put the string in a sufficiently sized array in
3874 * writable memory. */
3875 char device_name[50];
3876 strcpy(device_name,"Direct3D HEL");
3878 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3880 if (!callback)
3881 return DDERR_INVALIDPARAMS;
3883 wined3d_mutex_lock();
3885 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &device_desc1, &device_desc7);
3886 if (hr != D3D_OK)
3888 wined3d_mutex_unlock();
3889 return hr;
3892 /* Do I have to enumerate the reference id? Note from old d3d7:
3893 * "It seems that enumerating the reference IID on Direct3D 1 games
3894 * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
3896 * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers,
3897 * EnumReference which enables / disables enumerating the reference
3898 * rasterizer. It's a DWORD, 0 means disabled, 2 means enabled. The
3899 * enablerefrast.reg and disablerefrast.reg files in the DirectX 7.0 sdk
3900 * demo directory suggest this.
3902 * Some games(GTA 2) seem to use the second enumerated device, so I have
3903 * to enumerate at least 2 devices. So enumerate the reference device to
3904 * have 2 devices.
3906 * Other games (Rollcage) tell emulation and hal device apart by certain
3907 * flags. Rollcage expects D3DPTEXTURECAPS_POW2 to be set (yeah, it is a
3908 * limitation flag), and it refuses all devices that have the perspective
3909 * flag set. This way it refuses the emulation device, and HAL devices
3910 * never have POW2 unset in d3d7 on windows. */
3911 if (ddraw->d3dversion != 1)
3913 static CHAR reference_description[] = "RGB Direct3D emulation";
3915 TRACE("Enumerating WineD3D D3DDevice interface.\n");
3916 hal_desc = device_desc1;
3917 hel_desc = device_desc1;
3918 /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps. */
3919 hal_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3920 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3921 hal_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3922 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3923 /* RGB, RAMP and MMX devices have a HAL dcmColorModel of 0 */
3924 hal_desc.dcmColorModel = 0;
3926 hr = callback((GUID *)&IID_IDirect3DRGBDevice, reference_description,
3927 device_name, &hal_desc, &hel_desc, context);
3928 if (hr != D3DENUMRET_OK)
3930 TRACE("Application cancelled the enumeration.\n");
3931 wined3d_mutex_unlock();
3932 return D3D_OK;
3936 strcpy(device_name,"Direct3D HAL");
3938 TRACE("Enumerating HAL Direct3D device.\n");
3939 hal_desc = device_desc1;
3940 hel_desc = device_desc1;
3942 /* The hal device does not have the pow2 flag set in hel, but in hal. */
3943 hel_desc.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3944 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3945 hel_desc.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2
3946 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
3947 /* HAL devices have a HEL dcmColorModel of 0 */
3948 hel_desc.dcmColorModel = 0;
3950 hr = callback((GUID *)&IID_IDirect3DHALDevice, wined3d_description,
3951 device_name, &hal_desc, &hel_desc, context);
3952 if (hr != D3DENUMRET_OK)
3954 TRACE("Application cancelled the enumeration.\n");
3955 wined3d_mutex_unlock();
3956 return D3D_OK;
3959 TRACE("End of enumeration.\n");
3961 wined3d_mutex_unlock();
3963 return D3D_OK;
3966 static HRESULT WINAPI d3d2_EnumDevices(IDirect3D2 *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3968 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
3970 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3972 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3975 static HRESULT WINAPI d3d1_EnumDevices(IDirect3D *iface, LPD3DENUMDEVICESCALLBACK callback, void *context)
3977 struct ddraw *ddraw = impl_from_IDirect3D(iface);
3979 TRACE("iface %p, callback %p, context %p.\n", iface, callback, context);
3981 return d3d3_EnumDevices(&ddraw->IDirect3D3_iface, callback, context);
3984 /*****************************************************************************
3985 * IDirect3D3::CreateLight
3987 * Creates an IDirect3DLight interface. This interface is used in
3988 * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
3989 * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
3990 * uses the IDirect3DDevice7 interface with D3D7 lights.
3992 * Version 1, 2 and 3
3994 * Params:
3995 * light: Address to store the new interface pointer
3996 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
3997 * Must be NULL
3999 * Returns:
4000 * D3D_OK on success
4001 * DDERR_OUTOFMEMORY if memory allocation failed
4002 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4004 *****************************************************************************/
4005 static HRESULT WINAPI d3d3_CreateLight(IDirect3D3 *iface, IDirect3DLight **light,
4006 IUnknown *outer_unknown)
4008 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4009 struct d3d_light *object;
4011 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4013 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4015 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4016 if (!object)
4018 ERR("Failed to allocate light memory.\n");
4019 return DDERR_OUTOFMEMORY;
4022 d3d_light_init(object, ddraw);
4024 TRACE("Created light %p.\n", object);
4025 *light = &object->IDirect3DLight_iface;
4027 return D3D_OK;
4030 static HRESULT WINAPI d3d2_CreateLight(IDirect3D2 *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4032 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4034 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4036 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
4039 static HRESULT WINAPI d3d1_CreateLight(IDirect3D *iface, IDirect3DLight **light, IUnknown *outer_unknown)
4041 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4043 TRACE("iface %p, light %p, outer_unknown %p.\n", iface, light, outer_unknown);
4045 return d3d3_CreateLight(&ddraw->IDirect3D3_iface, light, outer_unknown);
4048 /*****************************************************************************
4049 * IDirect3D3::CreateMaterial
4051 * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
4052 * and older versions. The IDirect3DMaterial implementation wraps its
4053 * functionality to IDirect3DDevice7::SetMaterial and friends.
4055 * Version 1, 2 and 3
4057 * Params:
4058 * material: Address to store the new interface's pointer to
4059 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4060 * Must be NULL
4062 * Returns:
4063 * D3D_OK on success
4064 * DDERR_OUTOFMEMORY if memory allocation failed
4065 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4067 *****************************************************************************/
4068 static HRESULT WINAPI d3d3_CreateMaterial(IDirect3D3 *iface, IDirect3DMaterial3 **material,
4069 IUnknown *outer_unknown)
4071 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4072 struct d3d_material *object;
4074 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4076 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4078 object = d3d_material_create(ddraw);
4079 if (!object)
4081 ERR("Failed to allocate material memory.\n");
4082 return DDERR_OUTOFMEMORY;
4085 TRACE("Created material %p.\n", object);
4086 *material = &object->IDirect3DMaterial3_iface;
4088 return D3D_OK;
4091 static HRESULT WINAPI d3d2_CreateMaterial(IDirect3D2 *iface, IDirect3DMaterial2 **material,
4092 IUnknown *outer_unknown)
4094 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4095 struct d3d_material *object;
4097 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4099 object = d3d_material_create(ddraw);
4100 if (!object)
4102 ERR("Failed to allocate material memory.\n");
4103 return DDERR_OUTOFMEMORY;
4106 TRACE("Created material %p.\n", object);
4107 *material = &object->IDirect3DMaterial2_iface;
4109 return D3D_OK;
4112 static HRESULT WINAPI d3d1_CreateMaterial(IDirect3D *iface, IDirect3DMaterial **material,
4113 IUnknown *outer_unknown)
4115 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4116 struct d3d_material *object;
4118 TRACE("iface %p, material %p, outer_unknown %p.\n", iface, material, outer_unknown);
4120 object = d3d_material_create(ddraw);
4121 if (!object)
4123 ERR("Failed to allocate material memory.\n");
4124 return DDERR_OUTOFMEMORY;
4127 TRACE("Created material %p.\n", object);
4128 *material = &object->IDirect3DMaterial_iface;
4130 return D3D_OK;
4133 /*****************************************************************************
4134 * IDirect3D3::CreateViewport
4136 * Creates an IDirect3DViewport interface. This interface is used
4137 * by Direct3D and earlier versions for Viewport management. In Direct3D7
4138 * it has been replaced by a viewport structure and
4139 * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
4140 * uses the IDirect3DDevice7 methods for its functionality
4142 * Params:
4143 * Viewport: Address to store the new interface pointer
4144 * outer_unknown: Basically for aggregation, but ddraw doesn't support it.
4145 * Must be NULL
4147 * Returns:
4148 * D3D_OK on success
4149 * DDERR_OUTOFMEMORY if memory allocation failed
4150 * CLASS_E_NOAGGREGATION if outer_unknown != NULL
4152 *****************************************************************************/
4153 static HRESULT WINAPI d3d3_CreateViewport(IDirect3D3 *iface, IDirect3DViewport3 **viewport,
4154 IUnknown *outer_unknown)
4156 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4157 struct d3d_viewport *object;
4159 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4161 if (outer_unknown) return CLASS_E_NOAGGREGATION;
4163 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
4164 if (!object)
4166 ERR("Failed to allocate viewport memory.\n");
4167 return DDERR_OUTOFMEMORY;
4170 d3d_viewport_init(object, ddraw);
4172 TRACE("Created viewport %p.\n", object);
4173 *viewport = &object->IDirect3DViewport3_iface;
4175 return D3D_OK;
4178 static HRESULT WINAPI d3d2_CreateViewport(IDirect3D2 *iface, IDirect3DViewport2 **viewport, IUnknown *outer_unknown)
4180 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4182 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4184 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4185 outer_unknown);
4188 static HRESULT WINAPI d3d1_CreateViewport(IDirect3D *iface, IDirect3DViewport **viewport, IUnknown *outer_unknown)
4190 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4192 TRACE("iface %p, viewport %p, outer_unknown %p.\n", iface, viewport, outer_unknown);
4194 return d3d3_CreateViewport(&ddraw->IDirect3D3_iface, (IDirect3DViewport3 **)viewport,
4195 outer_unknown);
4198 /*****************************************************************************
4199 * IDirect3D3::FindDevice
4201 * This method finds a device with the requested properties and returns a
4202 * device description
4204 * Verion 1, 2 and 3
4205 * Params:
4206 * fds: Describes the requested device characteristics
4207 * fdr: Returns the device description
4209 * Returns:
4210 * D3D_OK on success
4211 * DDERR_INVALIDPARAMS if no device was found
4213 *****************************************************************************/
4214 static HRESULT WINAPI d3d3_FindDevice(IDirect3D3 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4216 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4217 D3DDEVICEDESC7 desc7;
4218 D3DDEVICEDESC desc1;
4219 HRESULT hr;
4221 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4223 if (!fds || !fdr) return DDERR_INVALIDPARAMS;
4225 if (fds->dwSize != sizeof(D3DFINDDEVICESEARCH)
4226 || fdr->dwSize != sizeof(D3DFINDDEVICERESULT))
4227 return DDERR_INVALIDPARAMS;
4229 if ((fds->dwFlags & D3DFDS_COLORMODEL)
4230 && fds->dcmColorModel != D3DCOLOR_RGB)
4232 WARN("Trying to request a non-RGB D3D color model. Not supported.\n");
4233 return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
4236 if (fds->dwFlags & D3DFDS_GUID)
4238 TRACE("Trying to match guid %s.\n", debugstr_guid(&(fds->guid)));
4239 if (!IsEqualGUID(&IID_D3DDEVICE_WineD3D, &fds->guid)
4240 && !IsEqualGUID(&IID_IDirect3DHALDevice, &fds->guid)
4241 && !IsEqualGUID(&IID_IDirect3DRGBDevice, &fds->guid))
4243 WARN("No match for this GUID.\n");
4244 return DDERR_NOTFOUND;
4248 /* Get the caps */
4249 hr = IDirect3DImpl_GetCaps(ddraw->wined3d, &desc1, &desc7);
4250 if (hr != D3D_OK) return hr;
4252 /* Now return our own GUID */
4253 fdr->guid = IID_D3DDEVICE_WineD3D;
4254 fdr->ddHwDesc = desc1;
4255 fdr->ddSwDesc = desc1;
4257 TRACE("Returning Wine's wined3d device with (undumped) capabilities.\n");
4259 return D3D_OK;
4262 static HRESULT WINAPI d3d2_FindDevice(IDirect3D2 *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4264 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4266 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4268 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4271 static HRESULT WINAPI d3d1_FindDevice(IDirect3D *iface, D3DFINDDEVICESEARCH *fds, D3DFINDDEVICERESULT *fdr)
4273 struct ddraw *ddraw = impl_from_IDirect3D(iface);
4275 TRACE("iface %p, fds %p, fdr %p.\n", iface, fds, fdr);
4277 return d3d3_FindDevice(&ddraw->IDirect3D3_iface, fds, fdr);
4280 /*****************************************************************************
4281 * IDirect3D7::CreateDevice
4283 * Creates an IDirect3DDevice7 interface.
4285 * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
4286 * DirectDraw surfaces and are created with
4287 * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
4288 * create the device object and QueryInterfaces for IDirect3DDevice
4290 * Params:
4291 * refiid: IID of the device to create
4292 * Surface: Initial rendertarget
4293 * Device: Address to return the interface pointer
4295 * Returns:
4296 * D3D_OK on success
4297 * DDERR_OUTOFMEMORY if memory allocation failed
4298 * DDERR_INVALIDPARAMS if a device exists already
4300 *****************************************************************************/
4301 static HRESULT WINAPI d3d7_CreateDevice(IDirect3D7 *iface, REFCLSID riid,
4302 IDirectDrawSurface7 *surface, IDirect3DDevice7 **device)
4304 struct ddraw_surface *target = unsafe_impl_from_IDirectDrawSurface7(surface);
4305 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4306 struct d3d_device *object;
4307 HRESULT hr;
4309 TRACE("iface %p, riid %s, surface %p, device %p.\n", iface, debugstr_guid(riid), surface, device);
4311 wined3d_mutex_lock();
4312 hr = d3d_device_create(ddraw, target, 7, &object, NULL);
4313 if (SUCCEEDED(hr))
4314 *device = &object->IDirect3DDevice7_iface;
4315 else
4317 WARN("Failed to create device, hr %#x.\n", hr);
4318 *device = NULL;
4320 wined3d_mutex_unlock();
4322 return hr;
4325 static HRESULT WINAPI d3d3_CreateDevice(IDirect3D3 *iface, REFCLSID riid,
4326 IDirectDrawSurface4 *surface, IDirect3DDevice3 **device, IUnknown *outer_unknown)
4328 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface4(surface);
4329 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4330 struct d3d_device *device_impl;
4331 HRESULT hr;
4333 TRACE("iface %p, riid %s, surface %p, device %p, outer_unknown %p.\n",
4334 iface, debugstr_guid(riid), surface, device, outer_unknown);
4336 if (outer_unknown)
4337 return CLASS_E_NOAGGREGATION;
4339 wined3d_mutex_lock();
4340 hr = d3d_device_create(ddraw, surface_impl, 3, &device_impl, NULL);
4341 if (SUCCEEDED(hr))
4342 *device = &device_impl->IDirect3DDevice3_iface;
4343 else
4345 WARN("Failed to create device, hr %#x.\n", hr);
4346 *device = NULL;
4348 wined3d_mutex_unlock();
4350 return hr;
4353 static HRESULT WINAPI d3d2_CreateDevice(IDirect3D2 *iface, REFCLSID riid,
4354 IDirectDrawSurface *surface, IDirect3DDevice2 **device)
4356 struct ddraw_surface *surface_impl = unsafe_impl_from_IDirectDrawSurface(surface);
4357 struct ddraw *ddraw = impl_from_IDirect3D2(iface);
4358 struct d3d_device *device_impl;
4359 HRESULT hr;
4361 TRACE("iface %p, riid %s, surface %p, device %p.\n",
4362 iface, debugstr_guid(riid), surface, device);
4364 wined3d_mutex_lock();
4365 hr = d3d_device_create(ddraw, surface_impl, 2, &device_impl, NULL);
4366 if (SUCCEEDED(hr))
4367 *device = &device_impl->IDirect3DDevice2_iface;
4368 else
4370 WARN("Failed to create device, hr %#x.\n", hr);
4371 *device = NULL;
4373 wined3d_mutex_unlock();
4375 return hr;
4378 /*****************************************************************************
4379 * IDirect3D7::CreateVertexBuffer
4381 * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
4382 * interface.
4384 * Version 3 and 7
4386 * Params:
4387 * desc: Requested Vertex buffer properties
4388 * vertex_buffer: Address to return the interface pointer at
4389 * flags: Some flags, should be 0
4391 * Returns
4392 * D3D_OK on success
4393 * DDERR_OUTOFMEMORY if memory allocation failed
4394 * The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
4395 * DDERR_INVALIDPARAMS if desc or vertex_buffer are NULL
4397 *****************************************************************************/
4398 static HRESULT WINAPI d3d7_CreateVertexBuffer(IDirect3D7 *iface, D3DVERTEXBUFFERDESC *desc,
4399 IDirect3DVertexBuffer7 **vertex_buffer, DWORD flags)
4401 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4402 struct d3d_vertex_buffer *object;
4403 HRESULT hr;
4405 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x.\n",
4406 iface, desc, vertex_buffer, flags);
4408 if (!vertex_buffer || !desc) return DDERR_INVALIDPARAMS;
4410 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4411 if (hr == D3D_OK)
4413 TRACE("Created vertex buffer %p.\n", object);
4414 *vertex_buffer = &object->IDirect3DVertexBuffer7_iface;
4416 else
4417 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4419 return hr;
4422 static HRESULT WINAPI d3d3_CreateVertexBuffer(IDirect3D3 *iface, D3DVERTEXBUFFERDESC *desc,
4423 IDirect3DVertexBuffer **vertex_buffer, DWORD flags, IUnknown *outer_unknown)
4425 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4426 struct d3d_vertex_buffer *object;
4427 HRESULT hr;
4429 TRACE("iface %p, desc %p, vertex_buffer %p, flags %#x, outer_unknown %p.\n",
4430 iface, desc, vertex_buffer, flags, outer_unknown);
4432 if (outer_unknown)
4433 return CLASS_E_NOAGGREGATION;
4434 if (!vertex_buffer || !desc)
4435 return DDERR_INVALIDPARAMS;
4437 hr = d3d_vertex_buffer_create(&object, ddraw, desc);
4438 if (hr == D3D_OK)
4440 TRACE("Created vertex buffer %p.\n", object);
4441 *vertex_buffer = &object->IDirect3DVertexBuffer_iface;
4443 else
4444 WARN("Failed to create vertex buffer, hr %#x.\n", hr);
4446 return hr;
4449 /*****************************************************************************
4450 * IDirect3D7::EnumZBufferFormats
4452 * Enumerates all supported Z buffer pixel formats
4454 * Version 3 and 7
4456 * Params:
4457 * device_iid:
4458 * callback: callback to call for each pixel format
4459 * context: Pointer to pass back to the callback
4461 * Returns:
4462 * D3D_OK on success
4463 * DDERR_INVALIDPARAMS if callback is NULL
4464 * For details, see IWineD3DDevice::EnumZBufferFormats
4466 *****************************************************************************/
4467 static HRESULT WINAPI d3d7_EnumZBufferFormats(IDirect3D7 *iface, REFCLSID device_iid,
4468 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4470 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4471 struct wined3d_display_mode mode;
4472 enum wined3d_device_type type;
4473 unsigned int i;
4474 HRESULT hr;
4476 /* Order matters. Specifically, BattleZone II (full version) expects the
4477 * 16-bit depth formats to be listed before the 24 and 32 ones. */
4478 static const enum wined3d_format_id formats[] =
4480 WINED3DFMT_S1_UINT_D15_UNORM,
4481 WINED3DFMT_D16_UNORM,
4482 WINED3DFMT_X8D24_UNORM,
4483 WINED3DFMT_S4X4_UINT_D24_UNORM,
4484 WINED3DFMT_D24_UNORM_S8_UINT,
4485 WINED3DFMT_D32_UNORM,
4488 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4489 iface, debugstr_guid(device_iid), callback, context);
4491 if (!callback) return DDERR_INVALIDPARAMS;
4493 if (IsEqualGUID(device_iid, &IID_IDirect3DHALDevice)
4494 || IsEqualGUID(device_iid, &IID_IDirect3DTnLHalDevice)
4495 || IsEqualGUID(device_iid, &IID_D3DDEVICE_WineD3D))
4497 TRACE("Asked for HAL device.\n");
4498 type = WINED3D_DEVICE_TYPE_HAL;
4500 else if (IsEqualGUID(device_iid, &IID_IDirect3DRGBDevice)
4501 || IsEqualGUID(device_iid, &IID_IDirect3DMMXDevice))
4503 TRACE("Asked for SW device.\n");
4504 type = WINED3D_DEVICE_TYPE_SW;
4506 else if (IsEqualGUID(device_iid, &IID_IDirect3DRefDevice))
4508 TRACE("Asked for REF device.\n");
4509 type = WINED3D_DEVICE_TYPE_REF;
4511 else if (IsEqualGUID(device_iid, &IID_IDirect3DNullDevice))
4513 TRACE("Asked for NULLREF device.\n");
4514 type = WINED3D_DEVICE_TYPE_NULLREF;
4516 else
4518 FIXME("Unexpected device GUID %s.\n", debugstr_guid(device_iid));
4519 type = WINED3D_DEVICE_TYPE_HAL;
4522 wined3d_mutex_lock();
4523 /* We need an adapter format from somewhere to please wined3d and WGL.
4524 * Use the current display mode. So far all cards offer the same depth
4525 * stencil format for all modes, but if some do not and applications do
4526 * not like that we'll have to find some workaround, like iterating over
4527 * all imaginable formats and collecting all the depth stencil formats we
4528 * can get. */
4529 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
4531 ERR("Failed to get display mode, hr %#x.\n", hr);
4532 wined3d_mutex_unlock();
4533 return hr;
4536 for (i = 0; i < (sizeof(formats) / sizeof(*formats)); ++i)
4538 hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4539 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, formats[i], WINED3D_SURFACE_TYPE_OPENGL);
4540 if (SUCCEEDED(hr))
4542 DDPIXELFORMAT pformat;
4544 memset(&pformat, 0, sizeof(pformat));
4545 pformat.dwSize = sizeof(pformat);
4546 PixelFormat_WineD3DtoDD(&pformat, formats[i]);
4548 TRACE("Enumerating wined3d format %#x.\n", formats[i]);
4549 hr = callback(&pformat, context);
4550 if (hr != DDENUMRET_OK)
4552 TRACE("Format enumeration cancelled by application.\n");
4553 wined3d_mutex_unlock();
4554 return D3D_OK;
4559 /* Historically some windows drivers used dwZBufferBitDepth=24 for WINED3DFMT_X8D24_UNORM,
4560 * while others used dwZBufferBitDepth=32. In either case the pitch matches a 32 bits per
4561 * pixel format, so we use dwZBufferBitDepth=32. Some games expect 24. Windows Vista and
4562 * newer enumerate both versions, so we do the same(bug 22434) */
4563 hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT, type, mode.format_id,
4564 WINED3DUSAGE_DEPTHSTENCIL, WINED3D_RTYPE_SURFACE, WINED3DFMT_X8D24_UNORM, WINED3D_SURFACE_TYPE_OPENGL);
4565 if (SUCCEEDED(hr))
4567 DDPIXELFORMAT x8d24 =
4569 sizeof(x8d24), DDPF_ZBUFFER, 0,
4570 {24}, {0x00000000}, {0x00ffffff}, {0x00000000}
4572 TRACE("Enumerating WINED3DFMT_X8D24_UNORM, dwZBufferBitDepth=24 version\n");
4573 callback(&x8d24, context);
4576 TRACE("End of enumeration.\n");
4578 wined3d_mutex_unlock();
4580 return D3D_OK;
4583 static HRESULT WINAPI d3d3_EnumZBufferFormats(IDirect3D3 *iface, REFCLSID device_iid,
4584 LPD3DENUMPIXELFORMATSCALLBACK callback, void *context)
4586 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4588 TRACE("iface %p, device_iid %s, callback %p, context %p.\n",
4589 iface, debugstr_guid(device_iid), callback, context);
4591 return d3d7_EnumZBufferFormats(&ddraw->IDirect3D7_iface, device_iid, callback, context);
4594 /*****************************************************************************
4595 * IDirect3D7::EvictManagedTextures
4597 * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
4598 * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
4600 * Version 3 and 7
4602 * Returns:
4603 * D3D_OK, because it's a stub
4605 *****************************************************************************/
4606 static HRESULT WINAPI d3d7_EvictManagedTextures(IDirect3D7 *iface)
4608 struct ddraw *ddraw = impl_from_IDirect3D7(iface);
4610 TRACE("iface %p!\n", iface);
4612 wined3d_mutex_lock();
4613 if (ddraw->d3d_initialized)
4614 wined3d_device_evict_managed_resources(ddraw->wined3d_device);
4615 wined3d_mutex_unlock();
4617 return D3D_OK;
4620 static HRESULT WINAPI d3d3_EvictManagedTextures(IDirect3D3 *iface)
4622 struct ddraw *ddraw = impl_from_IDirect3D3(iface);
4624 TRACE("iface %p.\n", iface);
4626 return d3d7_EvictManagedTextures(&ddraw->IDirect3D7_iface);
4629 /*****************************************************************************
4630 * IDirect3DImpl_GetCaps
4632 * This function retrieves the device caps from wined3d
4633 * and converts it into a D3D7 and D3D - D3D3 structure
4634 * This is a helper function called from various places in ddraw
4636 * Params:
4637 * wined3d: The interface to get the caps from
4638 * desc1: Old D3D <3 structure to fill (needed)
4639 * desc7: D3D7 device desc structure to fill (needed)
4641 * Returns
4642 * D3D_OK on success, or the return value of IWineD3D::GetCaps
4644 *****************************************************************************/
4645 HRESULT IDirect3DImpl_GetCaps(const struct wined3d *wined3d, D3DDEVICEDESC *desc1, D3DDEVICEDESC7 *desc7)
4647 WINED3DCAPS wined3d_caps;
4648 HRESULT hr;
4650 TRACE("wined3d %p, desc1 %p, desc7 %p.\n", wined3d, desc1, desc7);
4652 memset(&wined3d_caps, 0, sizeof(wined3d_caps));
4654 wined3d_mutex_lock();
4655 hr = wined3d_get_device_caps(wined3d, 0, WINED3D_DEVICE_TYPE_HAL, &wined3d_caps);
4656 wined3d_mutex_unlock();
4657 if (FAILED(hr))
4659 WARN("Failed to get device caps, hr %#x.\n", hr);
4660 return hr;
4663 /* Copy the results into the d3d7 and d3d3 structures */
4664 desc7->dwDevCaps = wined3d_caps.DevCaps;
4665 desc7->dpcLineCaps.dwMiscCaps = wined3d_caps.PrimitiveMiscCaps;
4666 desc7->dpcLineCaps.dwRasterCaps = wined3d_caps.RasterCaps;
4667 desc7->dpcLineCaps.dwZCmpCaps = wined3d_caps.ZCmpCaps;
4668 desc7->dpcLineCaps.dwSrcBlendCaps = wined3d_caps.SrcBlendCaps;
4669 desc7->dpcLineCaps.dwDestBlendCaps = wined3d_caps.DestBlendCaps;
4670 desc7->dpcLineCaps.dwAlphaCmpCaps = wined3d_caps.AlphaCmpCaps;
4671 desc7->dpcLineCaps.dwShadeCaps = wined3d_caps.ShadeCaps;
4672 desc7->dpcLineCaps.dwTextureCaps = wined3d_caps.TextureCaps;
4673 desc7->dpcLineCaps.dwTextureFilterCaps = wined3d_caps.TextureFilterCaps;
4674 desc7->dpcLineCaps.dwTextureAddressCaps = wined3d_caps.TextureAddressCaps;
4676 desc7->dwMaxTextureWidth = wined3d_caps.MaxTextureWidth;
4677 desc7->dwMaxTextureHeight = wined3d_caps.MaxTextureHeight;
4679 desc7->dwMaxTextureRepeat = wined3d_caps.MaxTextureRepeat;
4680 desc7->dwMaxTextureAspectRatio = wined3d_caps.MaxTextureAspectRatio;
4681 desc7->dwMaxAnisotropy = wined3d_caps.MaxAnisotropy;
4682 desc7->dvMaxVertexW = wined3d_caps.MaxVertexW;
4684 desc7->dvGuardBandLeft = wined3d_caps.GuardBandLeft;
4685 desc7->dvGuardBandTop = wined3d_caps.GuardBandTop;
4686 desc7->dvGuardBandRight = wined3d_caps.GuardBandRight;
4687 desc7->dvGuardBandBottom = wined3d_caps.GuardBandBottom;
4689 desc7->dvExtentsAdjust = wined3d_caps.ExtentsAdjust;
4690 desc7->dwStencilCaps = wined3d_caps.StencilCaps;
4692 desc7->dwFVFCaps = wined3d_caps.FVFCaps;
4693 desc7->dwTextureOpCaps = wined3d_caps.TextureOpCaps;
4695 desc7->dwVertexProcessingCaps = wined3d_caps.VertexProcessingCaps;
4696 desc7->dwMaxActiveLights = wined3d_caps.MaxActiveLights;
4698 /* Remove all non-d3d7 caps */
4699 desc7->dwDevCaps &= (
4700 D3DDEVCAPS_FLOATTLVERTEX | D3DDEVCAPS_SORTINCREASINGZ | D3DDEVCAPS_SORTDECREASINGZ |
4701 D3DDEVCAPS_SORTEXACT | D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY |
4702 D3DDEVCAPS_TLVERTEXSYSTEMMEMORY | D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY |
4703 D3DDEVCAPS_TEXTUREVIDEOMEMORY | D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP |
4704 D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
4705 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT | D3DDEVCAPS_CANBLTSYSTONONLOCAL |
4706 D3DDEVCAPS_HWRASTERIZATION);
4708 desc7->dwStencilCaps &= (
4709 D3DSTENCILCAPS_KEEP | D3DSTENCILCAPS_ZERO | D3DSTENCILCAPS_REPLACE |
4710 D3DSTENCILCAPS_INCRSAT | D3DSTENCILCAPS_DECRSAT | D3DSTENCILCAPS_INVERT |
4711 D3DSTENCILCAPS_INCR | D3DSTENCILCAPS_DECR);
4713 /* FVF caps ?*/
4715 desc7->dwTextureOpCaps &= (
4716 D3DTEXOPCAPS_DISABLE | D3DTEXOPCAPS_SELECTARG1 | D3DTEXOPCAPS_SELECTARG2 |
4717 D3DTEXOPCAPS_MODULATE | D3DTEXOPCAPS_MODULATE2X | D3DTEXOPCAPS_MODULATE4X |
4718 D3DTEXOPCAPS_ADD | D3DTEXOPCAPS_ADDSIGNED | D3DTEXOPCAPS_ADDSIGNED2X |
4719 D3DTEXOPCAPS_SUBTRACT | D3DTEXOPCAPS_ADDSMOOTH | D3DTEXOPCAPS_BLENDTEXTUREALPHA |
4720 D3DTEXOPCAPS_BLENDFACTORALPHA | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM | D3DTEXOPCAPS_BLENDCURRENTALPHA |
4721 D3DTEXOPCAPS_PREMODULATE | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
4722 D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP |
4723 D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
4725 desc7->dwVertexProcessingCaps &= (
4726 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_VERTEXFOG |
4727 D3DVTXPCAPS_DIRECTIONALLIGHTS | D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER);
4729 desc7->dpcLineCaps.dwMiscCaps &= (
4730 D3DPMISCCAPS_MASKPLANES | D3DPMISCCAPS_MASKZ | D3DPMISCCAPS_LINEPATTERNREP |
4731 D3DPMISCCAPS_CONFORMANT | D3DPMISCCAPS_CULLNONE | D3DPMISCCAPS_CULLCW |
4732 D3DPMISCCAPS_CULLCCW);
4734 desc7->dpcLineCaps.dwRasterCaps &= (
4735 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ROP2 | D3DPRASTERCAPS_XOR |
4736 D3DPRASTERCAPS_PAT | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_SUBPIXEL |
4737 D3DPRASTERCAPS_SUBPIXELX | D3DPRASTERCAPS_FOGVERTEX | D3DPRASTERCAPS_FOGTABLE |
4738 D3DPRASTERCAPS_STIPPLE | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
4739 D3DPRASTERCAPS_ANTIALIASEDGES | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBIAS |
4740 D3DPRASTERCAPS_ZBUFFERLESSHSR | D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY |
4741 D3DPRASTERCAPS_WBUFFER | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG |
4742 D3DPRASTERCAPS_ZFOG);
4744 desc7->dpcLineCaps.dwZCmpCaps &= (
4745 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4746 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4747 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4749 desc7->dpcLineCaps.dwSrcBlendCaps &= (
4750 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4751 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4752 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4753 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4754 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4756 desc7->dpcLineCaps.dwDestBlendCaps &= (
4757 D3DPBLENDCAPS_ZERO | D3DPBLENDCAPS_ONE | D3DPBLENDCAPS_SRCCOLOR |
4758 D3DPBLENDCAPS_INVSRCCOLOR | D3DPBLENDCAPS_SRCALPHA | D3DPBLENDCAPS_INVSRCALPHA |
4759 D3DPBLENDCAPS_DESTALPHA | D3DPBLENDCAPS_INVDESTALPHA | D3DPBLENDCAPS_DESTCOLOR |
4760 D3DPBLENDCAPS_INVDESTCOLOR | D3DPBLENDCAPS_SRCALPHASAT | D3DPBLENDCAPS_BOTHSRCALPHA |
4761 D3DPBLENDCAPS_BOTHINVSRCALPHA);
4763 desc7->dpcLineCaps.dwAlphaCmpCaps &= (
4764 D3DPCMPCAPS_NEVER | D3DPCMPCAPS_LESS | D3DPCMPCAPS_EQUAL |
4765 D3DPCMPCAPS_LESSEQUAL | D3DPCMPCAPS_GREATER | D3DPCMPCAPS_NOTEQUAL |
4766 D3DPCMPCAPS_GREATEREQUAL | D3DPCMPCAPS_ALWAYS);
4768 desc7->dpcLineCaps.dwShadeCaps &= (
4769 D3DPSHADECAPS_COLORFLATMONO | D3DPSHADECAPS_COLORFLATRGB | D3DPSHADECAPS_COLORGOURAUDMONO |
4770 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_COLORPHONGMONO | D3DPSHADECAPS_COLORPHONGRGB |
4771 D3DPSHADECAPS_SPECULARFLATMONO | D3DPSHADECAPS_SPECULARFLATRGB | D3DPSHADECAPS_SPECULARGOURAUDMONO |
4772 D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO | D3DPSHADECAPS_SPECULARPHONGRGB |
4773 D3DPSHADECAPS_ALPHAFLATBLEND | D3DPSHADECAPS_ALPHAFLATSTIPPLED | D3DPSHADECAPS_ALPHAGOURAUDBLEND |
4774 D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND | D3DPSHADECAPS_ALPHAPHONGSTIPPLED |
4775 D3DPSHADECAPS_FOGFLAT | D3DPSHADECAPS_FOGGOURAUD | D3DPSHADECAPS_FOGPHONG);
4777 desc7->dpcLineCaps.dwTextureCaps &= (
4778 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
4779 D3DPTEXTURECAPS_TRANSPARENCY | D3DPTEXTURECAPS_BORDER | D3DPTEXTURECAPS_SQUAREONLY |
4780 D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
4781 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_COLORKEYBLEND);
4783 desc7->dpcLineCaps.dwTextureFilterCaps &= (
4784 D3DPTFILTERCAPS_NEAREST | D3DPTFILTERCAPS_LINEAR | D3DPTFILTERCAPS_MIPNEAREST |
4785 D3DPTFILTERCAPS_MIPLINEAR | D3DPTFILTERCAPS_LINEARMIPNEAREST | D3DPTFILTERCAPS_LINEARMIPLINEAR |
4786 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
4787 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
4788 D3DPTFILTERCAPS_MAGFLINEAR | D3DPTFILTERCAPS_MAGFANISOTROPIC | D3DPTFILTERCAPS_MAGFAFLATCUBIC |
4789 D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
4791 desc7->dpcLineCaps.dwTextureBlendCaps &= (
4792 D3DPTBLENDCAPS_DECAL | D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_DECALALPHA |
4793 D3DPTBLENDCAPS_MODULATEALPHA | D3DPTBLENDCAPS_DECALMASK | D3DPTBLENDCAPS_MODULATEMASK |
4794 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_ADD);
4796 desc7->dpcLineCaps.dwTextureAddressCaps &= (
4797 D3DPTADDRESSCAPS_WRAP | D3DPTADDRESSCAPS_MIRROR | D3DPTADDRESSCAPS_CLAMP |
4798 D3DPTADDRESSCAPS_BORDER | D3DPTADDRESSCAPS_INDEPENDENTUV);
4800 if (!(desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2))
4802 /* DirectX7 always has the np2 flag set, no matter what the card
4803 * supports. Some old games (Rollcage) check the caps incorrectly.
4804 * If wined3d supports nonpow2 textures it also has np2 conditional
4805 * support. */
4806 desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
4809 /* Fill the missing members, and do some fixup */
4810 desc7->dpcLineCaps.dwSize = sizeof(desc7->dpcLineCaps);
4811 desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
4812 D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
4813 D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
4814 D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
4815 desc7->dpcLineCaps.dwStippleWidth = 32;
4816 desc7->dpcLineCaps.dwStippleHeight = 32;
4817 /* Use the same for the TriCaps */
4818 desc7->dpcTriCaps = desc7->dpcLineCaps;
4820 desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
4821 desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
4822 desc7->dwMinTextureWidth = 1;
4823 desc7->dwMinTextureHeight = 1;
4825 /* Convert DWORDs safely to WORDs */
4826 if (wined3d_caps.MaxTextureBlendStages > 0xffff) desc7->wMaxTextureBlendStages = 0xffff;
4827 else desc7->wMaxTextureBlendStages = (WORD)wined3d_caps.MaxTextureBlendStages;
4828 if (wined3d_caps.MaxSimultaneousTextures > 0xffff) desc7->wMaxSimultaneousTextures = 0xffff;
4829 else desc7->wMaxSimultaneousTextures = (WORD)wined3d_caps.MaxSimultaneousTextures;
4831 if (wined3d_caps.MaxUserClipPlanes > 0xffff) desc7->wMaxUserClipPlanes = 0xffff;
4832 else desc7->wMaxUserClipPlanes = (WORD)wined3d_caps.MaxUserClipPlanes;
4833 if (wined3d_caps.MaxVertexBlendMatrices > 0xffff) desc7->wMaxVertexBlendMatrices = 0xffff;
4834 else desc7->wMaxVertexBlendMatrices = (WORD)wined3d_caps.MaxVertexBlendMatrices;
4836 desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
4838 desc7->dwReserved1 = 0;
4839 desc7->dwReserved2 = 0;
4840 desc7->dwReserved3 = 0;
4841 desc7->dwReserved4 = 0;
4843 /* Fill the old structure */
4844 memset(desc1, 0, sizeof(*desc1));
4845 desc1->dwSize = sizeof(D3DDEVICEDESC);
4846 desc1->dwFlags = D3DDD_COLORMODEL
4847 | D3DDD_DEVCAPS
4848 | D3DDD_TRANSFORMCAPS
4849 | D3DDD_BCLIPPING
4850 | D3DDD_LIGHTINGCAPS
4851 | D3DDD_LINECAPS
4852 | D3DDD_TRICAPS
4853 | D3DDD_DEVICERENDERBITDEPTH
4854 | D3DDD_DEVICEZBUFFERBITDEPTH
4855 | D3DDD_MAXBUFFERSIZE
4856 | D3DDD_MAXVERTEXCOUNT;
4858 desc1->dcmColorModel = D3DCOLOR_RGB;
4859 desc1->dwDevCaps = desc7->dwDevCaps;
4860 desc1->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
4861 desc1->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
4862 desc1->bClipping = TRUE;
4863 desc1->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
4864 desc1->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL
4865 | D3DLIGHTCAPS_PARALLELPOINT
4866 | D3DLIGHTCAPS_POINT
4867 | D3DLIGHTCAPS_SPOT;
4869 desc1->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
4870 desc1->dlcLightingCaps.dwNumLights = desc7->dwMaxActiveLights;
4872 desc1->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
4873 desc1->dpcLineCaps.dwMiscCaps = desc7->dpcLineCaps.dwMiscCaps;
4874 desc1->dpcLineCaps.dwRasterCaps = desc7->dpcLineCaps.dwRasterCaps;
4875 desc1->dpcLineCaps.dwZCmpCaps = desc7->dpcLineCaps.dwZCmpCaps;
4876 desc1->dpcLineCaps.dwSrcBlendCaps = desc7->dpcLineCaps.dwSrcBlendCaps;
4877 desc1->dpcLineCaps.dwDestBlendCaps = desc7->dpcLineCaps.dwDestBlendCaps;
4878 desc1->dpcLineCaps.dwShadeCaps = desc7->dpcLineCaps.dwShadeCaps;
4879 desc1->dpcLineCaps.dwTextureCaps = desc7->dpcLineCaps.dwTextureCaps;
4880 desc1->dpcLineCaps.dwTextureFilterCaps = desc7->dpcLineCaps.dwTextureFilterCaps;
4881 desc1->dpcLineCaps.dwTextureBlendCaps = desc7->dpcLineCaps.dwTextureBlendCaps;
4882 desc1->dpcLineCaps.dwTextureAddressCaps = desc7->dpcLineCaps.dwTextureAddressCaps;
4883 desc1->dpcLineCaps.dwStippleWidth = desc7->dpcLineCaps.dwStippleWidth;
4884 desc1->dpcLineCaps.dwAlphaCmpCaps = desc7->dpcLineCaps.dwAlphaCmpCaps;
4886 desc1->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
4887 desc1->dpcTriCaps.dwMiscCaps = desc7->dpcTriCaps.dwMiscCaps;
4888 desc1->dpcTriCaps.dwRasterCaps = desc7->dpcTriCaps.dwRasterCaps;
4889 desc1->dpcTriCaps.dwZCmpCaps = desc7->dpcTriCaps.dwZCmpCaps;
4890 desc1->dpcTriCaps.dwSrcBlendCaps = desc7->dpcTriCaps.dwSrcBlendCaps;
4891 desc1->dpcTriCaps.dwDestBlendCaps = desc7->dpcTriCaps.dwDestBlendCaps;
4892 desc1->dpcTriCaps.dwShadeCaps = desc7->dpcTriCaps.dwShadeCaps;
4893 desc1->dpcTriCaps.dwTextureCaps = desc7->dpcTriCaps.dwTextureCaps;
4894 desc1->dpcTriCaps.dwTextureFilterCaps = desc7->dpcTriCaps.dwTextureFilterCaps;
4895 desc1->dpcTriCaps.dwTextureBlendCaps = desc7->dpcTriCaps.dwTextureBlendCaps;
4896 desc1->dpcTriCaps.dwTextureAddressCaps = desc7->dpcTriCaps.dwTextureAddressCaps;
4897 desc1->dpcTriCaps.dwStippleWidth = desc7->dpcTriCaps.dwStippleWidth;
4898 desc1->dpcTriCaps.dwAlphaCmpCaps = desc7->dpcTriCaps.dwAlphaCmpCaps;
4900 desc1->dwDeviceRenderBitDepth = desc7->dwDeviceRenderBitDepth;
4901 desc1->dwDeviceZBufferBitDepth = desc7->dwDeviceZBufferBitDepth;
4902 desc1->dwMaxBufferSize = 0;
4903 desc1->dwMaxVertexCount = 65536;
4904 desc1->dwMinTextureWidth = desc7->dwMinTextureWidth;
4905 desc1->dwMinTextureHeight = desc7->dwMinTextureHeight;
4906 desc1->dwMaxTextureWidth = desc7->dwMaxTextureWidth;
4907 desc1->dwMaxTextureHeight = desc7->dwMaxTextureHeight;
4908 desc1->dwMinStippleWidth = 1;
4909 desc1->dwMinStippleHeight = 1;
4910 desc1->dwMaxStippleWidth = 32;
4911 desc1->dwMaxStippleHeight = 32;
4912 desc1->dwMaxTextureRepeat = desc7->dwMaxTextureRepeat;
4913 desc1->dwMaxTextureAspectRatio = desc7->dwMaxTextureAspectRatio;
4914 desc1->dwMaxAnisotropy = desc7->dwMaxAnisotropy;
4915 desc1->dvGuardBandLeft = desc7->dvGuardBandLeft;
4916 desc1->dvGuardBandRight = desc7->dvGuardBandRight;
4917 desc1->dvGuardBandTop = desc7->dvGuardBandTop;
4918 desc1->dvGuardBandBottom = desc7->dvGuardBandBottom;
4919 desc1->dvExtentsAdjust = desc7->dvExtentsAdjust;
4920 desc1->dwStencilCaps = desc7->dwStencilCaps;
4921 desc1->dwFVFCaps = desc7->dwFVFCaps;
4922 desc1->dwTextureOpCaps = desc7->dwTextureOpCaps;
4923 desc1->wMaxTextureBlendStages = desc7->wMaxTextureBlendStages;
4924 desc1->wMaxSimultaneousTextures = desc7->wMaxSimultaneousTextures;
4926 return DD_OK;
4929 /*****************************************************************************
4930 * IDirectDraw7 VTable
4931 *****************************************************************************/
4932 static const struct IDirectDraw7Vtbl ddraw7_vtbl =
4934 /* IUnknown */
4935 ddraw7_QueryInterface,
4936 ddraw7_AddRef,
4937 ddraw7_Release,
4938 /* IDirectDraw */
4939 ddraw7_Compact,
4940 ddraw7_CreateClipper,
4941 ddraw7_CreatePalette,
4942 ddraw7_CreateSurface,
4943 ddraw7_DuplicateSurface,
4944 ddraw7_EnumDisplayModes,
4945 ddraw7_EnumSurfaces,
4946 ddraw7_FlipToGDISurface,
4947 ddraw7_GetCaps,
4948 ddraw7_GetDisplayMode,
4949 ddraw7_GetFourCCCodes,
4950 ddraw7_GetGDISurface,
4951 ddraw7_GetMonitorFrequency,
4952 ddraw7_GetScanLine,
4953 ddraw7_GetVerticalBlankStatus,
4954 ddraw7_Initialize,
4955 ddraw7_RestoreDisplayMode,
4956 ddraw7_SetCooperativeLevel,
4957 ddraw7_SetDisplayMode,
4958 ddraw7_WaitForVerticalBlank,
4959 /* IDirectDraw2 */
4960 ddraw7_GetAvailableVidMem,
4961 /* IDirectDraw3 */
4962 ddraw7_GetSurfaceFromDC,
4963 /* IDirectDraw4 */
4964 ddraw7_RestoreAllSurfaces,
4965 ddraw7_TestCooperativeLevel,
4966 ddraw7_GetDeviceIdentifier,
4967 /* IDirectDraw7 */
4968 ddraw7_StartModeTest,
4969 ddraw7_EvaluateMode
4972 static const struct IDirectDraw4Vtbl ddraw4_vtbl =
4974 /* IUnknown */
4975 ddraw4_QueryInterface,
4976 ddraw4_AddRef,
4977 ddraw4_Release,
4978 /* IDirectDraw */
4979 ddraw4_Compact,
4980 ddraw4_CreateClipper,
4981 ddraw4_CreatePalette,
4982 ddraw4_CreateSurface,
4983 ddraw4_DuplicateSurface,
4984 ddraw4_EnumDisplayModes,
4985 ddraw4_EnumSurfaces,
4986 ddraw4_FlipToGDISurface,
4987 ddraw4_GetCaps,
4988 ddraw4_GetDisplayMode,
4989 ddraw4_GetFourCCCodes,
4990 ddraw4_GetGDISurface,
4991 ddraw4_GetMonitorFrequency,
4992 ddraw4_GetScanLine,
4993 ddraw4_GetVerticalBlankStatus,
4994 ddraw4_Initialize,
4995 ddraw4_RestoreDisplayMode,
4996 ddraw4_SetCooperativeLevel,
4997 ddraw4_SetDisplayMode,
4998 ddraw4_WaitForVerticalBlank,
4999 /* IDirectDraw2 */
5000 ddraw4_GetAvailableVidMem,
5001 /* IDirectDraw3 */
5002 ddraw4_GetSurfaceFromDC,
5003 /* IDirectDraw4 */
5004 ddraw4_RestoreAllSurfaces,
5005 ddraw4_TestCooperativeLevel,
5006 ddraw4_GetDeviceIdentifier,
5009 static const struct IDirectDraw2Vtbl ddraw2_vtbl =
5011 /* IUnknown */
5012 ddraw2_QueryInterface,
5013 ddraw2_AddRef,
5014 ddraw2_Release,
5015 /* IDirectDraw */
5016 ddraw2_Compact,
5017 ddraw2_CreateClipper,
5018 ddraw2_CreatePalette,
5019 ddraw2_CreateSurface,
5020 ddraw2_DuplicateSurface,
5021 ddraw2_EnumDisplayModes,
5022 ddraw2_EnumSurfaces,
5023 ddraw2_FlipToGDISurface,
5024 ddraw2_GetCaps,
5025 ddraw2_GetDisplayMode,
5026 ddraw2_GetFourCCCodes,
5027 ddraw2_GetGDISurface,
5028 ddraw2_GetMonitorFrequency,
5029 ddraw2_GetScanLine,
5030 ddraw2_GetVerticalBlankStatus,
5031 ddraw2_Initialize,
5032 ddraw2_RestoreDisplayMode,
5033 ddraw2_SetCooperativeLevel,
5034 ddraw2_SetDisplayMode,
5035 ddraw2_WaitForVerticalBlank,
5036 /* IDirectDraw2 */
5037 ddraw2_GetAvailableVidMem,
5040 static const struct IDirectDrawVtbl ddraw1_vtbl =
5042 /* IUnknown */
5043 ddraw1_QueryInterface,
5044 ddraw1_AddRef,
5045 ddraw1_Release,
5046 /* IDirectDraw */
5047 ddraw1_Compact,
5048 ddraw1_CreateClipper,
5049 ddraw1_CreatePalette,
5050 ddraw1_CreateSurface,
5051 ddraw1_DuplicateSurface,
5052 ddraw1_EnumDisplayModes,
5053 ddraw1_EnumSurfaces,
5054 ddraw1_FlipToGDISurface,
5055 ddraw1_GetCaps,
5056 ddraw1_GetDisplayMode,
5057 ddraw1_GetFourCCCodes,
5058 ddraw1_GetGDISurface,
5059 ddraw1_GetMonitorFrequency,
5060 ddraw1_GetScanLine,
5061 ddraw1_GetVerticalBlankStatus,
5062 ddraw1_Initialize,
5063 ddraw1_RestoreDisplayMode,
5064 ddraw1_SetCooperativeLevel,
5065 ddraw1_SetDisplayMode,
5066 ddraw1_WaitForVerticalBlank,
5069 static const struct IDirect3D7Vtbl d3d7_vtbl =
5071 /* IUnknown methods */
5072 d3d7_QueryInterface,
5073 d3d7_AddRef,
5074 d3d7_Release,
5075 /* IDirect3D7 methods */
5076 d3d7_EnumDevices,
5077 d3d7_CreateDevice,
5078 d3d7_CreateVertexBuffer,
5079 d3d7_EnumZBufferFormats,
5080 d3d7_EvictManagedTextures
5083 static const struct IDirect3D3Vtbl d3d3_vtbl =
5085 /* IUnknown methods */
5086 d3d3_QueryInterface,
5087 d3d3_AddRef,
5088 d3d3_Release,
5089 /* IDirect3D3 methods */
5090 d3d3_EnumDevices,
5091 d3d3_CreateLight,
5092 d3d3_CreateMaterial,
5093 d3d3_CreateViewport,
5094 d3d3_FindDevice,
5095 d3d3_CreateDevice,
5096 d3d3_CreateVertexBuffer,
5097 d3d3_EnumZBufferFormats,
5098 d3d3_EvictManagedTextures
5101 static const struct IDirect3D2Vtbl d3d2_vtbl =
5103 /* IUnknown methods */
5104 d3d2_QueryInterface,
5105 d3d2_AddRef,
5106 d3d2_Release,
5107 /* IDirect3D2 methods */
5108 d3d2_EnumDevices,
5109 d3d2_CreateLight,
5110 d3d2_CreateMaterial,
5111 d3d2_CreateViewport,
5112 d3d2_FindDevice,
5113 d3d2_CreateDevice
5116 static const struct IDirect3DVtbl d3d1_vtbl =
5118 /* IUnknown methods */
5119 d3d1_QueryInterface,
5120 d3d1_AddRef,
5121 d3d1_Release,
5122 /* IDirect3D methods */
5123 d3d1_Initialize,
5124 d3d1_EnumDevices,
5125 d3d1_CreateLight,
5126 d3d1_CreateMaterial,
5127 d3d1_CreateViewport,
5128 d3d1_FindDevice
5131 /*****************************************************************************
5132 * ddraw_find_decl
5134 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
5135 * if none was found.
5137 * This function is in ddraw.c and the DDraw object space because D3D7
5138 * vertex buffers are created using the IDirect3D interface to the ddraw
5139 * object, so they can be valid across D3D devices(theoretically. The ddraw
5140 * object also owns the wined3d device
5142 * Parameters:
5143 * This: Device
5144 * fvf: Fvf to find the decl for
5146 * Returns:
5147 * NULL in case of an error, the vertex declaration for the FVF otherwise.
5149 *****************************************************************************/
5150 struct wined3d_vertex_declaration *ddraw_find_decl(struct ddraw *This, DWORD fvf)
5152 struct wined3d_vertex_declaration *pDecl = NULL;
5153 HRESULT hr;
5154 int p, low, high; /* deliberately signed */
5155 struct FvfToDecl *convertedDecls = This->decls;
5157 TRACE("Searching for declaration for fvf %08x... ", fvf);
5159 low = 0;
5160 high = This->numConvertedDecls - 1;
5161 while(low <= high) {
5162 p = (low + high) >> 1;
5163 TRACE("%d ", p);
5164 if(convertedDecls[p].fvf == fvf) {
5165 TRACE("found %p\n", convertedDecls[p].decl);
5166 return convertedDecls[p].decl;
5167 } else if(convertedDecls[p].fvf < fvf) {
5168 low = p + 1;
5169 } else {
5170 high = p - 1;
5173 TRACE("not found. Creating and inserting at position %d.\n", low);
5175 hr = wined3d_vertex_declaration_create_from_fvf(This->wined3d_device,
5176 fvf, This, &ddraw_null_wined3d_parent_ops, &pDecl);
5177 if (hr != S_OK) return NULL;
5179 if(This->declArraySize == This->numConvertedDecls) {
5180 int grow = max(This->declArraySize / 2, 8);
5181 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
5182 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
5183 if (!convertedDecls)
5185 wined3d_vertex_declaration_decref(pDecl);
5186 return NULL;
5188 This->decls = convertedDecls;
5189 This->declArraySize += grow;
5192 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
5193 convertedDecls[low].decl = pDecl;
5194 convertedDecls[low].fvf = fvf;
5195 This->numConvertedDecls++;
5197 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
5198 return pDecl;
5201 static inline struct ddraw *ddraw_from_device_parent(struct wined3d_device_parent *device_parent)
5203 return CONTAINING_RECORD(device_parent, struct ddraw, device_parent);
5206 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
5207 struct wined3d_device *device)
5209 TRACE("device_parent %p, device %p.\n", device_parent, device);
5212 /* This is run from device_process_message() in wined3d, we can't take the
5213 * wined3d mutex. */
5214 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
5216 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5217 MONITORINFO monitor_info;
5218 HMONITOR monitor;
5219 BOOL ret;
5220 RECT *r;
5222 TRACE("device_parent %p.\n", device_parent);
5224 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE) || !ddraw->swapchain_window)
5226 TRACE("Nothing to resize.\n");
5227 return;
5230 monitor = MonitorFromWindow(ddraw->swapchain_window, MONITOR_DEFAULTTOPRIMARY);
5231 monitor_info.cbSize = sizeof(monitor_info);
5232 if (!(ret = GetMonitorInfoW(monitor, &monitor_info)))
5234 ERR("Failed to get monitor info.\n");
5235 return;
5238 r = &monitor_info.rcMonitor;
5239 TRACE("Resizing window %p to %s.\n", ddraw->swapchain_window, wine_dbgstr_rect(r));
5241 if (!(ret = SetWindowPos(ddraw->swapchain_window, HWND_TOP, r->left, r->top,
5242 r->right - r->left, r->bottom - r->top, SWP_SHOWWINDOW | SWP_NOACTIVATE)))
5243 ERR("Failed to resize window.\n");
5246 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
5247 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
5248 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
5250 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5251 struct ddraw_surface *tex_root = container_parent;
5252 DDSURFACEDESC2 desc = tex_root->surface_desc;
5253 struct ddraw_surface *ddraw_surface;
5254 HRESULT hr;
5256 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
5257 "\tpool %#x, level %u, face %u, surface %p.\n",
5258 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
5260 /* The ddraw root surface is created before the wined3d texture. */
5261 if (!level && face == WINED3D_CUBEMAP_FACE_POSITIVE_X)
5263 ddraw_surface = tex_root;
5264 goto done;
5267 desc.dwWidth = width;
5268 desc.dwHeight = height;
5269 if (level)
5270 desc.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
5271 else
5272 desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
5274 if (desc.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5276 desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
5278 switch (face)
5280 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
5281 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
5282 break;
5284 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
5285 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
5286 break;
5288 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
5289 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
5290 break;
5292 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
5293 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
5294 break;
5296 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
5297 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
5298 break;
5300 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
5301 desc.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
5302 break;
5304 default:
5305 ERR("Unexpected cube face.\n");
5306 return DDERR_INVALIDPARAMS;
5311 /* FIXME: Validate that format, usage, pool, etc. really make sense. */
5312 if (FAILED(hr = ddraw_create_surface(ddraw, &desc, &ddraw_surface, level, tex_root->version)))
5313 return hr;
5315 done:
5316 *surface = ddraw_surface->wined3d_surface;
5317 wined3d_surface_incref(*surface);
5319 return DD_OK;
5322 static void STDMETHODCALLTYPE ddraw_frontbuffer_destroyed(void *parent)
5324 struct ddraw *ddraw = parent;
5325 ddraw->wined3d_frontbuffer = NULL;
5328 static const struct wined3d_parent_ops ddraw_frontbuffer_parent_ops =
5330 ddraw_frontbuffer_destroyed,
5333 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
5334 void *container_parent, UINT width, UINT height, enum wined3d_format_id format_id, DWORD usage,
5335 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, struct wined3d_surface **surface)
5337 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5338 HRESULT hr;
5340 TRACE("device_parent %p, container_parent %p, width %u, height %u, format_id %#x, usage %#x,\n"
5341 "\tmultisample_type %#x, multisample_quality %u, surface %p.\n",
5342 device_parent, container_parent, width, height, format_id, usage,
5343 multisample_type, multisample_quality, surface);
5345 if (ddraw->wined3d_frontbuffer)
5347 ERR("Frontbuffer already created.\n");
5348 return E_FAIL;
5351 if (SUCCEEDED(hr = wined3d_surface_create(ddraw->wined3d_device, width, height, format_id, 0,
5352 usage, WINED3D_POOL_DEFAULT, multisample_type, multisample_quality, DefaultSurfaceType,
5353 WINED3D_SURFACE_MAPPABLE, ddraw, &ddraw_frontbuffer_parent_ops, surface)))
5354 ddraw->wined3d_frontbuffer = *surface;
5356 return hr;
5359 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
5360 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
5361 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
5363 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
5364 "format %#x, pool %#x, usage %#x, volume %p.\n",
5365 device_parent, container_parent, width, height, depth,
5366 format, pool, usage, volume);
5368 ERR("Not implemented!\n");
5370 return E_NOTIMPL;
5373 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
5374 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
5376 struct ddraw *ddraw = ddraw_from_device_parent(device_parent);
5377 HRESULT hr;
5379 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
5381 if (ddraw->wined3d_swapchain)
5383 ERR("Swapchain already created.\n");
5384 return E_FAIL;
5387 hr = wined3d_swapchain_create(ddraw->wined3d_device, desc,
5388 DefaultSurfaceType, NULL, &ddraw_null_wined3d_parent_ops, swapchain);
5389 if (FAILED(hr))
5390 WARN("Failed to create swapchain, hr %#x.\n", hr);
5392 return hr;
5395 static const struct wined3d_device_parent_ops ddraw_wined3d_device_parent_ops =
5397 device_parent_wined3d_device_created,
5398 device_parent_mode_changed,
5399 device_parent_create_swapchain_surface,
5400 device_parent_create_texture_surface,
5401 device_parent_create_volume,
5402 device_parent_create_swapchain,
5405 HRESULT ddraw_init(struct ddraw *ddraw, enum wined3d_device_type device_type)
5407 HRESULT hr;
5408 HDC hDC;
5410 ddraw->IDirectDraw7_iface.lpVtbl = &ddraw7_vtbl;
5411 ddraw->IDirectDraw_iface.lpVtbl = &ddraw1_vtbl;
5412 ddraw->IDirectDraw2_iface.lpVtbl = &ddraw2_vtbl;
5413 ddraw->IDirectDraw4_iface.lpVtbl = &ddraw4_vtbl;
5414 ddraw->IDirect3D_iface.lpVtbl = &d3d1_vtbl;
5415 ddraw->IDirect3D2_iface.lpVtbl = &d3d2_vtbl;
5416 ddraw->IDirect3D3_iface.lpVtbl = &d3d3_vtbl;
5417 ddraw->IDirect3D7_iface.lpVtbl = &d3d7_vtbl;
5418 ddraw->device_parent.ops = &ddraw_wined3d_device_parent_ops;
5419 ddraw->numIfaces = 1;
5420 ddraw->ref7 = 1;
5422 /* Get the current screen settings. */
5423 hDC = GetDC(0);
5424 ddraw->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
5425 ReleaseDC(0, hDC);
5426 ddraw->orig_width = GetSystemMetrics(SM_CXSCREEN);
5427 ddraw->orig_height = GetSystemMetrics(SM_CYSCREEN);
5429 ddraw->wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS);
5430 if (!ddraw->wined3d)
5432 WARN("Failed to create a wined3d object.\n");
5433 return E_OUTOFMEMORY;
5436 hr = wined3d_device_create(ddraw->wined3d, WINED3DADAPTER_DEFAULT, device_type,
5437 NULL, 0, 8, &ddraw->device_parent, &ddraw->wined3d_device);
5438 if (FAILED(hr))
5440 WARN("Failed to create a wined3d device, hr %#x.\n", hr);
5441 wined3d_decref(ddraw->wined3d);
5442 return hr;
5445 list_init(&ddraw->surface_list);
5447 return DD_OK;