d2d1: Create feature level 10.0 device context state objects.
[wine.git] / dlls / d3d9 / directx.c
bloba1ebc986226c46be628bedcbb91583ce37f0f04b
1 /*
2 * IDirect3D9 implementation
4 * Copyright 2002 Jason Edmeades
5 * Copyright 2005 Oliver Stieber
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "d3d9_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26 static inline struct d3d9 *impl_from_IDirect3D9Ex(IDirect3D9Ex *iface)
28 return CONTAINING_RECORD(iface, struct d3d9, IDirect3D9Ex_iface);
31 static HRESULT WINAPI d3d9_QueryInterface(IDirect3D9Ex *iface, REFIID riid, void **out)
33 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
35 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
37 if (IsEqualGUID(riid, &IID_IDirect3D9)
38 || IsEqualGUID(riid, &IID_IUnknown))
40 IDirect3D9Ex_AddRef(&d3d9->IDirect3D9Ex_iface);
41 *out = &d3d9->IDirect3D9Ex_iface;
42 return S_OK;
45 if (IsEqualGUID(riid, &IID_IDirect3D9Ex))
47 if (!d3d9->extended)
49 WARN("Application asks for IDirect3D9Ex, but this instance wasn't created with Direct3DCreate9Ex.\n");
50 *out = NULL;
51 return E_NOINTERFACE;
54 IDirect3D9Ex_AddRef(&d3d9->IDirect3D9Ex_iface);
55 *out = &d3d9->IDirect3D9Ex_iface;
56 return S_OK;
59 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
61 *out = NULL;
62 return E_NOINTERFACE;
65 static ULONG WINAPI d3d9_AddRef(IDirect3D9Ex *iface)
67 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
68 ULONG refcount = InterlockedIncrement(&d3d9->refcount);
70 TRACE("%p increasing refcount to %u.\n", iface, refcount);
72 return refcount;
75 static ULONG WINAPI d3d9_Release(IDirect3D9Ex *iface)
77 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
78 ULONG refcount = InterlockedDecrement(&d3d9->refcount);
80 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
82 if (!refcount)
84 wined3d_decref(d3d9->wined3d);
86 heap_free(d3d9->wined3d_outputs);
87 heap_free(d3d9);
90 return refcount;
93 static HRESULT WINAPI d3d9_RegisterSoftwareDevice(IDirect3D9Ex *iface, void *init_function)
95 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
96 HRESULT hr;
98 TRACE("iface %p, init_function %p.\n", iface, init_function);
100 wined3d_mutex_lock();
101 hr = wined3d_register_software_device(d3d9->wined3d, init_function);
102 wined3d_mutex_unlock();
104 return hr;
107 static UINT WINAPI d3d9_GetAdapterCount(IDirect3D9Ex *iface)
109 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
111 TRACE("iface %p.\n", iface);
113 return d3d9->wined3d_output_count;
116 static HRESULT WINAPI d3d9_GetAdapterIdentifier(IDirect3D9Ex *iface, UINT adapter,
117 DWORD flags, D3DADAPTER_IDENTIFIER9 *identifier)
119 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
120 struct wined3d_adapter_identifier adapter_id;
121 struct wined3d_adapter *wined3d_adapter;
122 struct wined3d_output_desc output_desc;
123 unsigned int output_idx;
124 HRESULT hr;
126 TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
127 iface, adapter, flags, identifier);
129 output_idx = adapter;
130 if (output_idx >= d3d9->wined3d_output_count)
131 return D3DERR_INVALIDCALL;
133 wined3d_mutex_lock();
134 if (FAILED(hr = wined3d_output_get_desc(d3d9->wined3d_outputs[output_idx], &output_desc)))
136 wined3d_mutex_unlock();
137 WARN("Failed to get output description, hr %#x.\n", hr);
138 return hr;
140 WideCharToMultiByte(CP_ACP, 0, output_desc.device_name, -1, identifier->DeviceName,
141 sizeof(identifier->DeviceName), NULL, NULL);
143 adapter_id.driver = identifier->Driver;
144 adapter_id.driver_size = sizeof(identifier->Driver);
145 adapter_id.description = identifier->Description;
146 adapter_id.description_size = sizeof(identifier->Description);
148 wined3d_adapter = wined3d_output_get_adapter(d3d9->wined3d_outputs[output_idx]);
149 if (SUCCEEDED(hr = wined3d_adapter_get_identifier(wined3d_adapter, flags, &adapter_id)))
151 identifier->DriverVersion = adapter_id.driver_version;
152 identifier->VendorId = adapter_id.vendor_id;
153 identifier->DeviceId = adapter_id.device_id;
154 identifier->SubSysId = adapter_id.subsystem_id;
155 identifier->Revision = adapter_id.revision;
156 memcpy(&identifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(identifier->DeviceIdentifier));
157 identifier->WHQLLevel = adapter_id.whql_level;
159 wined3d_mutex_unlock();
161 return hr;
164 static UINT WINAPI d3d9_GetAdapterModeCount(IDirect3D9Ex *iface, UINT adapter, D3DFORMAT format)
166 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
167 unsigned int output_idx, count;
169 TRACE("iface %p, adapter %u, format %#x.\n", iface, adapter, format);
171 output_idx = adapter;
172 if (output_idx >= d3d9->wined3d_output_count)
173 return 0;
175 /* Others than that not supported by d3d9, but reported by wined3d for ddraw. Filter them out. */
176 if (format != D3DFMT_X8R8G8B8 && format != D3DFMT_R5G6B5)
177 return 0;
179 wined3d_mutex_lock();
180 count = wined3d_output_get_mode_count(d3d9->wined3d_outputs[output_idx],
181 wined3dformat_from_d3dformat(format), WINED3D_SCANLINE_ORDERING_UNKNOWN);
182 wined3d_mutex_unlock();
184 return count;
187 static HRESULT WINAPI d3d9_EnumAdapterModes(IDirect3D9Ex *iface, UINT adapter,
188 D3DFORMAT format, UINT mode_idx, D3DDISPLAYMODE *mode)
190 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
191 struct wined3d_display_mode wined3d_mode;
192 unsigned int output_idx;
193 HRESULT hr;
195 TRACE("iface %p, adapter %u, format %#x, mode_idx %u, mode %p.\n",
196 iface, adapter, format, mode_idx, mode);
198 output_idx = adapter;
199 if (output_idx >= d3d9->wined3d_output_count)
200 return D3DERR_INVALIDCALL;
202 if (format != D3DFMT_X8R8G8B8 && format != D3DFMT_R5G6B5)
203 return D3DERR_INVALIDCALL;
205 wined3d_mutex_lock();
206 hr = wined3d_output_get_mode(d3d9->wined3d_outputs[output_idx],
207 wined3dformat_from_d3dformat(format), WINED3D_SCANLINE_ORDERING_UNKNOWN, mode_idx,
208 &wined3d_mode);
209 wined3d_mutex_unlock();
211 if (SUCCEEDED(hr))
213 mode->Width = wined3d_mode.width;
214 mode->Height = wined3d_mode.height;
215 mode->RefreshRate = wined3d_mode.refresh_rate;
216 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
219 return hr;
222 static HRESULT WINAPI d3d9_GetAdapterDisplayMode(IDirect3D9Ex *iface, UINT adapter, D3DDISPLAYMODE *mode)
224 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
225 struct wined3d_display_mode wined3d_mode;
226 unsigned int output_idx;
227 HRESULT hr;
229 TRACE("iface %p, adapter %u, mode %p.\n", iface, adapter, mode);
231 output_idx = adapter;
232 if (output_idx >= d3d9->wined3d_output_count)
233 return D3DERR_INVALIDCALL;
235 wined3d_mutex_lock();
236 hr = wined3d_output_get_display_mode(d3d9->wined3d_outputs[output_idx], &wined3d_mode, NULL);
237 wined3d_mutex_unlock();
239 if (SUCCEEDED(hr))
241 mode->Width = wined3d_mode.width;
242 mode->Height = wined3d_mode.height;
243 mode->RefreshRate = wined3d_mode.refresh_rate;
244 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
247 return hr;
250 static HRESULT WINAPI d3d9_CheckDeviceType(IDirect3D9Ex *iface, UINT adapter, D3DDEVTYPE device_type,
251 D3DFORMAT display_format, D3DFORMAT backbuffer_format, BOOL windowed)
253 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
254 unsigned int output_idx;
255 HRESULT hr;
257 TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
258 iface, adapter, device_type, display_format, backbuffer_format, windowed);
260 output_idx = adapter;
261 if (output_idx >= d3d9->wined3d_output_count)
262 return D3DERR_INVALIDCALL;
264 /* Others than that not supported by d3d9, but reported by wined3d for ddraw. Filter them out. */
265 if (!windowed && display_format != D3DFMT_X8R8G8B8 && display_format != D3DFMT_R5G6B5)
266 return WINED3DERR_NOTAVAILABLE;
268 wined3d_mutex_lock();
269 hr = wined3d_check_device_type(d3d9->wined3d, d3d9->wined3d_outputs[output_idx],
270 wined3d_device_type_from_d3d(device_type), wined3dformat_from_d3dformat(display_format),
271 wined3dformat_from_d3dformat(backbuffer_format), windowed);
272 wined3d_mutex_unlock();
274 return hr;
277 static HRESULT WINAPI d3d9_CheckDeviceFormat(IDirect3D9Ex *iface, UINT adapter, D3DDEVTYPE device_type,
278 D3DFORMAT adapter_format, DWORD usage, D3DRESOURCETYPE resource_type, D3DFORMAT format)
280 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
281 enum wined3d_resource_type wined3d_rtype;
282 struct wined3d_adapter *wined3d_adapter;
283 unsigned int bind_flags;
284 unsigned int output_idx;
285 HRESULT hr;
287 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
288 iface, adapter, device_type, adapter_format, usage, resource_type, format);
290 output_idx = adapter;
291 if (output_idx >= d3d9->wined3d_output_count)
292 return D3DERR_INVALIDCALL;
294 if (adapter_format != D3DFMT_X8R8G8B8 && adapter_format != D3DFMT_R5G6B5
295 && adapter_format != D3DFMT_X1R5G5B5)
297 WARN("Invalid adapter format.\n");
298 return adapter_format ? D3DERR_NOTAVAILABLE : D3DERR_INVALIDCALL;
301 bind_flags = wined3d_bind_flags_from_d3d9_usage(usage);
302 usage = usage & (WINED3DUSAGE_MASK | WINED3DUSAGE_QUERY_MASK);
303 switch (resource_type)
305 case D3DRTYPE_CUBETEXTURE:
306 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
307 case D3DRTYPE_TEXTURE:
308 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
309 case D3DRTYPE_SURFACE:
310 wined3d_rtype = WINED3D_RTYPE_TEXTURE_2D;
311 break;
313 case D3DRTYPE_VOLUMETEXTURE:
314 case D3DRTYPE_VOLUME:
315 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
316 wined3d_rtype = WINED3D_RTYPE_TEXTURE_3D;
317 break;
319 case D3DRTYPE_VERTEXBUFFER:
320 case D3DRTYPE_INDEXBUFFER:
321 wined3d_rtype = WINED3D_RTYPE_BUFFER;
322 break;
324 default:
325 FIXME("Unhandled resource type %#x.\n", resource_type);
326 return WINED3DERR_INVALIDCALL;
329 wined3d_mutex_lock();
330 wined3d_adapter = wined3d_output_get_adapter(d3d9->wined3d_outputs[output_idx]);
331 if (format == D3DFMT_RESZ && resource_type == D3DRTYPE_SURFACE && usage == D3DUSAGE_RENDERTARGET)
333 DWORD levels;
335 hr = wined3d_check_device_multisample_type(wined3d_adapter, wined3d_device_type_from_d3d(device_type),
336 WINED3DFMT_D24_UNORM_S8_UINT, FALSE, WINED3D_MULTISAMPLE_NON_MASKABLE, &levels);
337 if (SUCCEEDED(hr) && !levels)
338 hr = D3DERR_NOTAVAILABLE;
340 else
341 hr = wined3d_check_device_format(d3d9->wined3d, wined3d_adapter, wined3d_device_type_from_d3d(device_type),
342 wined3dformat_from_d3dformat(adapter_format), usage, bind_flags,
343 wined3d_rtype, wined3dformat_from_d3dformat(format));
344 wined3d_mutex_unlock();
346 return hr;
349 static HRESULT WINAPI d3d9_CheckDeviceMultiSampleType(IDirect3D9Ex *iface, UINT adapter, D3DDEVTYPE device_type,
350 D3DFORMAT format, BOOL windowed, D3DMULTISAMPLE_TYPE multisample_type, DWORD *levels)
352 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
353 struct wined3d_adapter *wined3d_adapter;
354 unsigned int output_idx;
355 HRESULT hr;
357 TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x, levels %p.\n",
358 iface, adapter, device_type, format, windowed, multisample_type, levels);
360 output_idx = adapter;
361 if (output_idx >= d3d9->wined3d_output_count)
362 return D3DERR_INVALIDCALL;
364 if (multisample_type > D3DMULTISAMPLE_16_SAMPLES)
365 return D3DERR_INVALIDCALL;
367 wined3d_mutex_lock();
368 wined3d_adapter = wined3d_output_get_adapter(d3d9->wined3d_outputs[output_idx]);
369 hr = wined3d_check_device_multisample_type(wined3d_adapter,
370 wined3d_device_type_from_d3d(device_type), wined3dformat_from_d3dformat(format),
371 windowed, wined3d_multisample_type_from_d3d(multisample_type), levels);
372 wined3d_mutex_unlock();
373 if (hr == WINED3DERR_NOTAVAILABLE && levels)
374 *levels = 1;
376 return hr;
379 static HRESULT WINAPI d3d9_CheckDepthStencilMatch(IDirect3D9Ex *iface, UINT adapter, D3DDEVTYPE device_type,
380 D3DFORMAT adapter_format, D3DFORMAT rt_format, D3DFORMAT ds_format)
382 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
383 struct wined3d_adapter *wined3d_adapter;
384 unsigned int output_idx;
385 HRESULT hr;
387 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
388 iface, adapter, device_type, adapter_format, rt_format, ds_format);
390 output_idx = adapter;
391 if (output_idx >= d3d9->wined3d_output_count)
392 return D3DERR_INVALIDCALL;
394 wined3d_mutex_lock();
395 wined3d_adapter = wined3d_output_get_adapter(d3d9->wined3d_outputs[output_idx]);
396 hr = wined3d_check_depth_stencil_match(wined3d_adapter,
397 wined3d_device_type_from_d3d(device_type), wined3dformat_from_d3dformat(adapter_format),
398 wined3dformat_from_d3dformat(rt_format), wined3dformat_from_d3dformat(ds_format));
399 wined3d_mutex_unlock();
401 return hr;
404 static HRESULT WINAPI d3d9_CheckDeviceFormatConversion(IDirect3D9Ex *iface, UINT adapter,
405 D3DDEVTYPE device_type, D3DFORMAT src_format, D3DFORMAT dst_format)
407 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
408 unsigned int output_idx;
409 HRESULT hr;
411 TRACE("iface %p, adapter %u, device_type %#x, src_format %#x, dst_format %#x.\n",
412 iface, adapter, device_type, src_format, dst_format);
414 output_idx = adapter;
415 if (output_idx >= d3d9->wined3d_output_count)
416 return D3DERR_INVALIDCALL;
418 wined3d_mutex_lock();
419 hr = wined3d_check_device_format_conversion(d3d9->wined3d_outputs[output_idx],
420 wined3d_device_type_from_d3d(device_type), wined3dformat_from_d3dformat(src_format),
421 wined3dformat_from_d3dformat(dst_format));
422 wined3d_mutex_unlock();
424 return hr;
427 static HRESULT WINAPI d3d9_GetDeviceCaps(IDirect3D9Ex *iface, UINT adapter, D3DDEVTYPE device_type, D3DCAPS9 *caps)
429 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
430 struct wined3d_adapter *wined3d_adapter;
431 struct wined3d_caps wined3d_caps;
432 unsigned int output_idx;
433 HRESULT hr;
435 TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, adapter, device_type, caps);
437 output_idx = adapter;
438 if (output_idx >= d3d9->wined3d_output_count)
439 return D3DERR_INVALIDCALL;
441 if (!caps)
442 return D3DERR_INVALIDCALL;
444 memset(caps, 0, sizeof(*caps));
446 wined3d_mutex_lock();
447 wined3d_adapter = wined3d_output_get_adapter(d3d9->wined3d_outputs[output_idx]);
448 hr = wined3d_get_device_caps(wined3d_adapter, wined3d_device_type_from_d3d(device_type), &wined3d_caps);
449 wined3d_mutex_unlock();
451 d3d9_caps_from_wined3dcaps(d3d9, adapter, caps, &wined3d_caps);
453 return hr;
456 static HMONITOR WINAPI d3d9_GetAdapterMonitor(IDirect3D9Ex *iface, UINT adapter)
458 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
459 struct wined3d_output_desc desc;
460 unsigned int output_idx;
461 HRESULT hr;
463 TRACE("iface %p, adapter %u.\n", iface, adapter);
465 output_idx = adapter;
466 if (output_idx >= d3d9->wined3d_output_count)
467 return NULL;
469 wined3d_mutex_lock();
470 hr = wined3d_output_get_desc(d3d9->wined3d_outputs[output_idx], &desc);
471 wined3d_mutex_unlock();
473 if (FAILED(hr))
475 WARN("Failed to get output desc, hr %#x.\n", hr);
476 return NULL;
479 return desc.monitor;
482 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_CreateDevice(IDirect3D9Ex *iface, UINT adapter,
483 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
484 IDirect3DDevice9 **device)
486 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
487 struct d3d9_device *object;
488 HRESULT hr;
490 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
491 iface, adapter, device_type, focus_window, flags, parameters, device);
493 if (!(object = heap_alloc_zero(sizeof(*object))))
494 return E_OUTOFMEMORY;
496 hr = device_init(object, d3d9, d3d9->wined3d, adapter, device_type, focus_window, flags, parameters, NULL);
497 if (FAILED(hr))
499 WARN("Failed to initialize device, hr %#x.\n", hr);
500 heap_free(object);
501 return hr;
504 TRACE("Created device %p.\n", object);
505 *device = (IDirect3DDevice9 *)&object->IDirect3DDevice9Ex_iface;
507 return D3D_OK;
510 static UINT WINAPI d3d9_GetAdapterModeCountEx(IDirect3D9Ex *iface,
511 UINT adapter, const D3DDISPLAYMODEFILTER *filter)
513 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
514 unsigned int output_idx, count;
516 TRACE("iface %p, adapter %u, filter %p.\n", iface, adapter, filter);
518 output_idx = adapter;
519 if (output_idx >= d3d9->wined3d_output_count)
520 return 0;
522 if (filter->Format != D3DFMT_X8R8G8B8 && filter->Format != D3DFMT_R5G6B5)
523 return 0;
525 wined3d_mutex_lock();
526 count = wined3d_output_get_mode_count(d3d9->wined3d_outputs[output_idx],
527 wined3dformat_from_d3dformat(filter->Format), wined3d_scanline_ordering_from_d3d(filter->ScanLineOrdering));
528 wined3d_mutex_unlock();
530 return count;
533 static HRESULT WINAPI d3d9_EnumAdapterModesEx(IDirect3D9Ex *iface,
534 UINT adapter, const D3DDISPLAYMODEFILTER *filter, UINT mode_idx, D3DDISPLAYMODEEX *mode)
536 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
537 struct wined3d_display_mode wined3d_mode;
538 unsigned int output_idx;
539 HRESULT hr;
541 TRACE("iface %p, adapter %u, filter %p, mode_idx %u, mode %p.\n",
542 iface, adapter, filter, mode_idx, mode);
544 output_idx = adapter;
545 if (output_idx >= d3d9->wined3d_output_count)
546 return D3DERR_INVALIDCALL;
548 if (filter->Format != D3DFMT_X8R8G8B8 && filter->Format != D3DFMT_R5G6B5)
549 return D3DERR_INVALIDCALL;
551 wined3d_mutex_lock();
552 hr = wined3d_output_get_mode(d3d9->wined3d_outputs[output_idx], wined3dformat_from_d3dformat(filter->Format),
553 wined3d_scanline_ordering_from_d3d(filter->ScanLineOrdering), mode_idx, &wined3d_mode);
554 wined3d_mutex_unlock();
556 if (SUCCEEDED(hr))
558 mode->Width = wined3d_mode.width;
559 mode->Height = wined3d_mode.height;
560 mode->RefreshRate = wined3d_mode.refresh_rate;
561 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
562 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
565 return hr;
568 static HRESULT WINAPI d3d9_GetAdapterDisplayModeEx(IDirect3D9Ex *iface,
569 UINT adapter, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
571 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
572 struct wined3d_display_mode wined3d_mode;
573 unsigned int output_idx;
574 HRESULT hr;
576 TRACE("iface %p, adapter %u, mode %p, rotation %p.\n",
577 iface, adapter, mode, rotation);
579 output_idx = adapter;
580 if (output_idx >= d3d9->wined3d_output_count)
581 return D3DERR_INVALIDCALL;
583 if (mode->Size != sizeof(*mode))
584 return D3DERR_INVALIDCALL;
586 wined3d_mutex_lock();
587 hr = wined3d_output_get_display_mode(d3d9->wined3d_outputs[output_idx], &wined3d_mode,
588 (enum wined3d_display_rotation *)rotation);
589 wined3d_mutex_unlock();
591 if (SUCCEEDED(hr))
593 mode->Width = wined3d_mode.width;
594 mode->Height = wined3d_mode.height;
595 mode->RefreshRate = wined3d_mode.refresh_rate;
596 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
597 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
600 return hr;
603 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_CreateDeviceEx(IDirect3D9Ex *iface,
604 UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
605 D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode, IDirect3DDevice9Ex **device)
607 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
608 struct d3d9_device *object;
609 HRESULT hr;
611 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, mode %p, device %p.\n",
612 iface, adapter, device_type, focus_window, flags, parameters, mode, device);
614 if (!(object = heap_alloc_zero(sizeof(*object))))
615 return E_OUTOFMEMORY;
617 hr = device_init(object, d3d9, d3d9->wined3d, adapter, device_type, focus_window, flags, parameters, mode);
618 if (FAILED(hr))
620 WARN("Failed to initialize device, hr %#x.\n", hr);
621 heap_free(object);
622 return hr;
625 TRACE("Created device %p.\n", object);
626 *device = &object->IDirect3DDevice9Ex_iface;
628 return D3D_OK;
631 static HRESULT WINAPI d3d9_GetAdapterLUID(IDirect3D9Ex *iface, UINT adapter, LUID *luid)
633 struct d3d9 *d3d9 = impl_from_IDirect3D9Ex(iface);
634 struct wined3d_adapter_identifier adapter_id;
635 struct wined3d_adapter *wined3d_adapter;
636 unsigned int output_idx;
637 HRESULT hr;
639 TRACE("iface %p, adapter %u, luid %p.\n", iface, adapter, luid);
641 output_idx = adapter;
642 if (output_idx >= d3d9->wined3d_output_count)
643 return D3DERR_INVALIDCALL;
645 adapter_id.driver_size = 0;
646 adapter_id.description_size = 0;
648 wined3d_adapter = wined3d_output_get_adapter(d3d9->wined3d_outputs[output_idx]);
649 if (SUCCEEDED(hr = wined3d_adapter_get_identifier(wined3d_adapter, 0, &adapter_id)))
650 *luid = adapter_id.adapter_luid;
652 return hr;
655 static const struct IDirect3D9ExVtbl d3d9_vtbl =
657 /* IUnknown */
658 d3d9_QueryInterface,
659 d3d9_AddRef,
660 d3d9_Release,
661 /* IDirect3D9 */
662 d3d9_RegisterSoftwareDevice,
663 d3d9_GetAdapterCount,
664 d3d9_GetAdapterIdentifier,
665 d3d9_GetAdapterModeCount,
666 d3d9_EnumAdapterModes,
667 d3d9_GetAdapterDisplayMode,
668 d3d9_CheckDeviceType,
669 d3d9_CheckDeviceFormat,
670 d3d9_CheckDeviceMultiSampleType,
671 d3d9_CheckDepthStencilMatch,
672 d3d9_CheckDeviceFormatConversion,
673 d3d9_GetDeviceCaps,
674 d3d9_GetAdapterMonitor,
675 d3d9_CreateDevice,
676 /* IDirect3D9Ex */
677 d3d9_GetAdapterModeCountEx,
678 d3d9_EnumAdapterModesEx,
679 d3d9_GetAdapterDisplayModeEx,
680 d3d9_CreateDeviceEx,
681 d3d9_GetAdapterLUID,
684 BOOL d3d9_init(struct d3d9 *d3d9, BOOL extended)
686 DWORD flags = WINED3D_PRESENT_CONVERSION | WINED3D_HANDLE_RESTORE | WINED3D_PIXEL_CENTER_INTEGER
687 | WINED3D_SRGB_READ_WRITE_CONTROL | WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR
688 | WINED3D_NO_PRIMITIVE_RESTART | WINED3D_LEGACY_CUBEMAP_FILTERING
689 | WINED3D_NORMALIZED_DEPTH_BIAS;
690 unsigned int adapter_idx, output_idx, adapter_count, output_count = 0;
691 struct wined3d_adapter *wined3d_adapter;
693 if (!extended)
694 flags |= WINED3D_VIDMEM_ACCOUNTING;
695 else
696 flags |= WINED3D_RESTORE_MODE_ON_ACTIVATE;
698 d3d9->IDirect3D9Ex_iface.lpVtbl = &d3d9_vtbl;
699 d3d9->refcount = 1;
701 wined3d_mutex_lock();
702 d3d9->wined3d = wined3d_create(flags);
703 if (!d3d9->wined3d)
705 wined3d_mutex_unlock();
706 return FALSE;
709 adapter_count = wined3d_get_adapter_count(d3d9->wined3d);
710 for (adapter_idx = 0; adapter_idx < adapter_count; ++adapter_idx)
712 wined3d_adapter = wined3d_get_adapter(d3d9->wined3d, adapter_idx);
713 output_count += wined3d_adapter_get_output_count(wined3d_adapter);
716 d3d9->wined3d_outputs = heap_calloc(output_count, sizeof(*d3d9->wined3d_outputs));
717 if (!d3d9->wined3d_outputs)
719 wined3d_decref(d3d9->wined3d);
720 wined3d_mutex_unlock();
721 return FALSE;
724 d3d9->wined3d_output_count = 0;
725 for (adapter_idx = 0; adapter_idx < adapter_count; ++adapter_idx)
727 wined3d_adapter = wined3d_get_adapter(d3d9->wined3d, adapter_idx);
728 output_count = wined3d_adapter_get_output_count(wined3d_adapter);
729 for (output_idx = 0; output_idx < output_count; ++output_idx)
731 d3d9->wined3d_outputs[d3d9->wined3d_output_count] =
732 wined3d_adapter_get_output(wined3d_adapter, output_idx);
733 ++d3d9->wined3d_output_count;
737 wined3d_mutex_unlock();
738 d3d9->extended = extended;
740 return TRUE;