mf: Subscribe standard quality manager to clock state change events.
[wine/zf.git] / dlls / d3d11 / d3d11_main.c
blob6666876049d6c526c2186c33ee9595b0d9396971
1 /*
2 * Direct3D 11
4 * Copyright 2008 Henri Verbeet for CodeWeavers
5 * Copyright 2013 Austin English
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
23 #define D3D11_INIT_GUID
24 #include "d3d11_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
28 static const char *debug_d3d_driver_type(D3D_DRIVER_TYPE driver_type)
30 switch (driver_type)
32 #define D3D11_TO_STR(x) case x: return #x
33 D3D11_TO_STR(D3D_DRIVER_TYPE_UNKNOWN);
34 D3D11_TO_STR(D3D_DRIVER_TYPE_HARDWARE);
35 D3D11_TO_STR(D3D_DRIVER_TYPE_REFERENCE);
36 D3D11_TO_STR(D3D_DRIVER_TYPE_NULL);
37 D3D11_TO_STR(D3D_DRIVER_TYPE_SOFTWARE);
38 D3D11_TO_STR(D3D_DRIVER_TYPE_WARP);
39 #undef D3D11_TO_STR
40 default:
41 return wine_dbg_sprintf("Unrecognized D3D_DRIVER_TYPE %#x\n", driver_type);
45 static HRESULT WINAPI layer_init(enum dxgi_device_layer_id id, DWORD *count, DWORD *values)
47 TRACE("id %#x, count %p, values %p\n", id, count, values);
49 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
51 WARN("Unknown layer id %#x\n", id);
52 return E_NOTIMPL;
55 return S_OK;
58 static UINT WINAPI layer_get_size(enum dxgi_device_layer_id id, struct layer_get_size_args *args, DWORD unknown0)
60 TRACE("id %#x, args %p, unknown0 %#x\n", id, args, unknown0);
62 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
64 WARN("Unknown layer id %#x\n", id);
65 return 0;
68 return sizeof(struct d3d_device);
71 static HRESULT WINAPI layer_create(enum dxgi_device_layer_id id, void **layer_base, DWORD unknown0,
72 void *device_object, REFIID riid, void **device_layer)
74 struct d3d_device *object;
76 TRACE("id %#x, layer_base %p, unknown0 %#x, device_object %p, riid %s, device_layer %p\n",
77 id, layer_base, unknown0, device_object, debugstr_guid(riid), device_layer);
79 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
81 WARN("Unknown layer id %#x\n", id);
82 *device_layer = NULL;
83 return E_NOTIMPL;
86 object = *layer_base;
87 d3d_device_init(object, device_object);
88 *device_layer = &object->IUnknown_inner;
90 TRACE("Created d3d10 device at %p\n", object);
92 return S_OK;
95 HRESULT WINAPI D3D11CoreRegisterLayers(void)
97 static const struct dxgi_device_layer layers[] =
99 {DXGI_DEVICE_LAYER_D3D10_DEVICE, layer_init, layer_get_size, layer_create},
102 DXGID3D10RegisterLayers(layers, ARRAY_SIZE(layers));
104 return S_OK;
107 HRESULT WINAPI D3D11CoreCreateDevice(IDXGIFactory *factory, IDXGIAdapter *adapter, UINT flags,
108 const D3D_FEATURE_LEVEL *feature_levels, UINT levels, ID3D11Device **device)
110 struct d3d_device *d3d_device;
111 IUnknown *dxgi_device;
112 HMODULE d3d11;
113 HRESULT hr;
115 TRACE("factory %p, adapter %p, flags %#x, feature_levels %p, levels %u, device %p.\n",
116 factory, adapter, flags, feature_levels, levels, device);
118 d3d11 = GetModuleHandleA("d3d11.dll");
119 hr = DXGID3D10CreateDevice(d3d11, factory, adapter, flags, feature_levels, levels, (void **)&dxgi_device);
120 if (FAILED(hr))
122 WARN("Failed to create device, returning %#x.\n", hr);
123 return hr;
126 hr = IUnknown_QueryInterface(dxgi_device, &IID_ID3D11Device, (void **)device);
127 IUnknown_Release(dxgi_device);
128 if (FAILED(hr))
130 ERR("Failed to query ID3D11Device interface, returning E_FAIL.\n");
131 return E_FAIL;
134 if (!(d3d_device = impl_from_ID3D11Device(*device)))
136 ERR("Failed to retrieve device impl, returning E_FAIL.\n");
137 return E_FAIL;
139 d3d_device->d3d11_only = TRUE;
140 d3d_device->emulated_interface = IID_ID3D11Device2;
142 return S_OK;
145 static HRESULT d3d11_create_device(IDXGIAdapter *adapter, D3D_DRIVER_TYPE driver_type, HMODULE swrast, UINT flags,
146 const D3D_FEATURE_LEVEL *feature_levels, UINT levels, UINT sdk_version, ID3D11Device **device_out,
147 D3D_FEATURE_LEVEL *obtained_feature_level, ID3D11DeviceContext **immediate_context)
149 static const D3D_FEATURE_LEVEL default_feature_levels[] =
151 D3D_FEATURE_LEVEL_11_0,
152 D3D_FEATURE_LEVEL_10_1,
153 D3D_FEATURE_LEVEL_10_0,
154 D3D_FEATURE_LEVEL_9_3,
155 D3D_FEATURE_LEVEL_9_2,
156 D3D_FEATURE_LEVEL_9_1,
158 IDXGIFactory *factory;
159 ID3D11Device *device;
160 HRESULT hr;
162 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
163 "device %p, obtained_feature_level %p, immediate_context %p.\n",
164 adapter, debug_d3d_driver_type(driver_type), swrast, flags, feature_levels, levels, sdk_version,
165 device_out, obtained_feature_level, immediate_context);
167 if (device_out)
168 *device_out = NULL;
169 if (obtained_feature_level)
170 *obtained_feature_level = 0;
171 if (immediate_context)
172 *immediate_context = NULL;
174 if (adapter)
176 IDXGIAdapter_AddRef(adapter);
177 hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
178 if (FAILED(hr))
180 WARN("Failed to get dxgi factory, returning %#x.\n", hr);
181 return hr;
184 else
186 hr = CreateDXGIFactory1(&IID_IDXGIFactory, (void **)&factory);
187 if (FAILED(hr))
189 WARN("Failed to create dxgi factory, returning %#x.\n", hr);
190 return hr;
193 switch(driver_type)
195 case D3D_DRIVER_TYPE_WARP:
196 FIXME("WARP driver not implemented, falling back to hardware.\n");
197 case D3D_DRIVER_TYPE_HARDWARE:
199 hr = IDXGIFactory_EnumAdapters(factory, 0, &adapter);
200 if (FAILED(hr))
202 WARN("No adapters found, returning %#x.\n", hr);
203 IDXGIFactory_Release(factory);
204 return hr;
206 break;
209 case D3D_DRIVER_TYPE_NULL:
210 FIXME("NULL device not implemented, falling back to refrast.\n");
211 /* fall through, for now */
212 case D3D_DRIVER_TYPE_REFERENCE:
214 HMODULE refrast = LoadLibraryA("d3d11ref.dll");
215 if (!refrast)
217 WARN("Failed to load refrast, returning E_FAIL.\n");
218 IDXGIFactory_Release(factory);
219 return E_FAIL;
221 hr = IDXGIFactory_CreateSoftwareAdapter(factory, refrast, &adapter);
222 FreeLibrary(refrast);
223 if (FAILED(hr))
225 WARN("Failed to create a software adapter, returning %#x.\n", hr);
226 IDXGIFactory_Release(factory);
227 return hr;
229 break;
232 case D3D_DRIVER_TYPE_SOFTWARE:
234 if (!swrast)
236 WARN("Software device requested, but NULL swrast passed, returning E_FAIL.\n");
237 IDXGIFactory_Release(factory);
238 return E_FAIL;
240 hr = IDXGIFactory_CreateSoftwareAdapter(factory, swrast, &adapter);
241 if (FAILED(hr))
243 WARN("Failed to create a software adapter, returning %#x.\n", hr);
244 IDXGIFactory_Release(factory);
245 return hr;
247 break;
250 default:
251 FIXME("Unhandled driver type %#x.\n", driver_type);
252 IDXGIFactory_Release(factory);
253 return E_FAIL;
257 if (!feature_levels)
259 feature_levels = default_feature_levels;
260 levels = ARRAY_SIZE(default_feature_levels);
262 hr = D3D11CoreCreateDevice(factory, adapter, flags, feature_levels, levels, &device);
263 IDXGIAdapter_Release(adapter);
264 IDXGIFactory_Release(factory);
265 if (FAILED(hr))
267 WARN("Failed to create a device, returning %#x.\n", hr);
268 return hr;
271 TRACE("Created ID3D11Device %p.\n", device);
273 if (obtained_feature_level)
274 *obtained_feature_level = ID3D11Device_GetFeatureLevel(device);
276 if (immediate_context)
277 ID3D11Device_GetImmediateContext(device, immediate_context);
279 if (device_out)
280 *device_out = device;
281 else
282 ID3D11Device_Release(device);
284 return (device_out || immediate_context) ? S_OK : S_FALSE;
287 HRESULT WINAPI D3D11CreateDevice(IDXGIAdapter *adapter, D3D_DRIVER_TYPE driver_type, HMODULE swrast, UINT flags,
288 const D3D_FEATURE_LEVEL *feature_levels, UINT levels, UINT sdk_version, ID3D11Device **device_out,
289 D3D_FEATURE_LEVEL *obtained_feature_level, ID3D11DeviceContext **immediate_context)
291 return d3d11_create_device(adapter, driver_type, swrast, flags, feature_levels,
292 levels, sdk_version, device_out, obtained_feature_level, immediate_context);
295 HRESULT WINAPI D3D11CreateDeviceAndSwapChain(IDXGIAdapter *adapter, D3D_DRIVER_TYPE driver_type,
296 HMODULE swrast, UINT flags, const D3D_FEATURE_LEVEL *feature_levels, UINT levels,
297 UINT sdk_version, const DXGI_SWAP_CHAIN_DESC *swapchain_desc, IDXGISwapChain **swapchain,
298 ID3D11Device **device_out, D3D_FEATURE_LEVEL *obtained_feature_level, ID3D11DeviceContext **immediate_context)
300 DXGI_SWAP_CHAIN_DESC desc;
301 IDXGIDevice *dxgi_device;
302 IDXGIFactory *factory;
303 ID3D11Device *device;
304 HRESULT hr;
306 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
307 "swapchain_desc %p, swapchain %p, device %p, obtained_feature_level %p, immediate_context %p.\n",
308 adapter, debug_d3d_driver_type(driver_type), swrast, flags, feature_levels, levels, sdk_version,
309 swapchain_desc, swapchain, device_out, obtained_feature_level, immediate_context);
311 if (swapchain)
312 *swapchain = NULL;
313 if (device_out)
314 *device_out = NULL;
316 /* Avoid forwarding to D3D11CreateDevice(), since it breaks applications
317 * hooking these entry-points. */
318 if (FAILED(hr = d3d11_create_device(adapter, driver_type, swrast, flags, feature_levels,
319 levels, sdk_version, &device, obtained_feature_level, immediate_context)))
321 WARN("Failed to create a device, returning %#x.\n", hr);
322 return hr;
325 if (swapchain)
327 if (FAILED(hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (void **)&dxgi_device)))
329 ERR("Failed to get a dxgi device from the d3d11 device, returning %#x.\n", hr);
330 goto cleanup;
333 hr = IDXGIDevice_GetAdapter(dxgi_device, &adapter);
334 IDXGIDevice_Release(dxgi_device);
335 if (FAILED(hr))
337 ERR("Failed to get the device adapter, returning %#x.\n", hr);
338 goto cleanup;
341 hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
342 IDXGIAdapter_Release(adapter);
343 if (FAILED(hr))
345 ERR("Failed to get the adapter factory, returning %#x.\n", hr);
346 goto cleanup;
349 desc = *swapchain_desc;
350 hr = IDXGIFactory_CreateSwapChain(factory, (IUnknown *)device, &desc, swapchain);
351 IDXGIFactory_Release(factory);
352 if (FAILED(hr))
354 WARN("Failed to create a swapchain, returning %#x.\n", hr);
355 goto cleanup;
358 TRACE("Created IDXGISwapChain %p.\n", *swapchain);
361 if (device_out)
362 *device_out = device;
363 else
364 ID3D11Device_Release(device);
366 return (swapchain || device_out || immediate_context) ? S_OK : S_FALSE;
368 cleanup:
369 ID3D11Device_Release(device);
370 if (obtained_feature_level)
371 *obtained_feature_level = 0;
372 if (immediate_context)
374 ID3D11DeviceContext_Release(*immediate_context);
375 *immediate_context = NULL;
378 return hr;
381 HRESULT WINAPI D3D11On12CreateDevice(IUnknown *device, UINT flags,
382 const D3D_FEATURE_LEVEL *feature_levels, UINT feature_level_count,
383 IUnknown * const *queues, UINT queue_count, UINT node_mask,
384 ID3D11Device **d3d11_device, ID3D11DeviceContext **d3d11_immediate_context,
385 D3D_FEATURE_LEVEL *obtained_feature_level)
387 FIXME("device %p, flags %#x, feature_levels %p, feature_level_count %u, "
388 "queues %p, queue_count %u, node_mask 0x%08x, "
389 "d3d11_device %p, d3d11_immediate_context %p, obtained_feature_level %p stub!\n",
390 device, flags, feature_levels, feature_level_count, queues, queue_count,
391 node_mask, d3d11_device, d3d11_immediate_context, obtained_feature_level);
393 return E_NOTIMPL;