dxgi: Add feature level support.
[wine.git] / dlls / d3d11 / d3d11_main.c
blobafc161f220fc0c6dde529378598186918cedd8c8
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 #include "config.h"
24 #include "wine/port.h"
26 #define D3D11_INIT_GUID
27 #include "d3d11_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
31 static const char *debug_d3d_driver_type(D3D_DRIVER_TYPE driver_type)
33 switch (driver_type)
35 #define D3D11_TO_STR(x) case x: return #x
36 D3D11_TO_STR(D3D_DRIVER_TYPE_UNKNOWN);
37 D3D11_TO_STR(D3D_DRIVER_TYPE_HARDWARE);
38 D3D11_TO_STR(D3D_DRIVER_TYPE_REFERENCE);
39 D3D11_TO_STR(D3D_DRIVER_TYPE_NULL);
40 D3D11_TO_STR(D3D_DRIVER_TYPE_SOFTWARE);
41 D3D11_TO_STR(D3D_DRIVER_TYPE_WARP);
42 #undef D3D11_TO_STR
43 default:
44 return wine_dbg_sprintf("Unrecognized D3D_DRIVER_TYPE %#x\n", driver_type);
48 static HRESULT WINAPI layer_init(enum dxgi_device_layer_id id, DWORD *count, DWORD *values)
50 TRACE("id %#x, count %p, values %p\n", id, count, values);
52 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
54 WARN("Unknown layer id %#x\n", id);
55 return E_NOTIMPL;
58 return S_OK;
61 static UINT WINAPI layer_get_size(enum dxgi_device_layer_id id, struct layer_get_size_args *args, DWORD unknown0)
63 TRACE("id %#x, args %p, unknown0 %#x\n", id, args, unknown0);
65 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
67 WARN("Unknown layer id %#x\n", id);
68 return 0;
71 return sizeof(struct d3d_device);
74 static HRESULT WINAPI layer_create(enum dxgi_device_layer_id id, void **layer_base, DWORD unknown0,
75 void *device_object, REFIID riid, void **device_layer)
77 struct d3d_device *object;
78 HRESULT hr;
80 TRACE("id %#x, layer_base %p, unknown0 %#x, device_object %p, riid %s, device_layer %p\n",
81 id, layer_base, unknown0, device_object, debugstr_guid(riid), device_layer);
83 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
85 WARN("Unknown layer id %#x\n", id);
86 *device_layer = NULL;
87 return E_NOTIMPL;
90 object = *layer_base;
91 if (FAILED(hr = d3d_device_init(object, device_object)))
93 WARN("Failed to initialize device, hr %#x.\n", hr);
94 *device_layer = NULL;
95 return hr;
97 *device_layer = &object->IUnknown_inner;
99 TRACE("Created d3d10 device at %p\n", object);
101 return S_OK;
104 static void WINAPI layer_set_feature_level(enum dxgi_device_layer_id id, void *device,
105 D3D_FEATURE_LEVEL feature_level)
107 TRACE("id %#x, device %p, feature_level %u.\n", id, device, feature_level);
109 if (id != DXGI_DEVICE_LAYER_D3D10_DEVICE)
111 WARN("Unknown layer id %#x.\n", id);
112 return;
116 HRESULT WINAPI D3D11CoreRegisterLayers(void)
118 static const struct dxgi_device_layer layers[] =
120 {DXGI_DEVICE_LAYER_D3D10_DEVICE, layer_init, layer_get_size, layer_create, layer_set_feature_level},
123 DXGID3D10RegisterLayers(layers, sizeof(layers)/sizeof(*layers));
125 return S_OK;
128 HRESULT WINAPI D3D11CoreCreateDevice(IDXGIFactory *factory, IDXGIAdapter *adapter, UINT flags,
129 const D3D_FEATURE_LEVEL *feature_levels, UINT levels, ID3D11Device **device)
131 static const D3D_FEATURE_LEVEL level = D3D_FEATURE_LEVEL_10_0;
132 IUnknown *dxgi_device;
133 HMODULE d3d11;
134 HRESULT hr;
136 TRACE("factory %p, adapter %p, flags %#x, feature_levels %p, levels %u, device %p.\n",
137 factory, adapter, flags, feature_levels, levels, device);
139 FIXME("Ignoring feature levels.\n");
141 d3d11 = GetModuleHandleA("d3d11.dll");
142 hr = DXGID3D10CreateDevice(d3d11, factory, adapter, flags, &level, 1, (void **)&dxgi_device);
143 if (FAILED(hr))
145 WARN("Failed to create device, returning %#x.\n", hr);
146 return hr;
149 hr = IUnknown_QueryInterface(dxgi_device, &IID_ID3D11Device, (void **)device);
150 IUnknown_Release(dxgi_device);
151 if (FAILED(hr))
153 ERR("Failed to query ID3D11Device interface, returning E_FAIL.\n");
154 return E_FAIL;
157 return S_OK;
160 HRESULT WINAPI D3D11CreateDevice(IDXGIAdapter *adapter, D3D_DRIVER_TYPE driver_type, HMODULE swrast, UINT flags,
161 const D3D_FEATURE_LEVEL *feature_levels, UINT levels, UINT sdk_version, ID3D11Device **device_out,
162 D3D_FEATURE_LEVEL *obtained_feature_level, ID3D11DeviceContext **immediate_context)
164 IDXGIFactory *factory;
165 ID3D11Device *device;
166 HRESULT hr;
168 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
169 "device %p, obtained_feature_level %p, immediate_context %p.\n",
170 adapter, debug_d3d_driver_type(driver_type), swrast, flags, feature_levels, levels, sdk_version,
171 device_out, obtained_feature_level, immediate_context);
173 if (device_out)
174 *device_out = NULL;
175 if (obtained_feature_level)
176 *obtained_feature_level = 0;
177 if (immediate_context)
178 *immediate_context = NULL;
180 if (adapter)
182 IDXGIAdapter_AddRef(adapter);
183 hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
184 if (FAILED(hr))
186 WARN("Failed to get dxgi factory, returning %#x.\n", hr);
187 return hr;
190 else
192 hr = CreateDXGIFactory1(&IID_IDXGIFactory, (void **)&factory);
193 if (FAILED(hr))
195 WARN("Failed to create dxgi factory, returning %#x.\n", hr);
196 return hr;
199 switch(driver_type)
201 case D3D_DRIVER_TYPE_HARDWARE:
203 hr = IDXGIFactory_EnumAdapters(factory, 0, &adapter);
204 if (FAILED(hr))
206 WARN("No adapters found, returning %#x.\n", hr);
207 IDXGIFactory_Release(factory);
208 return hr;
210 break;
213 case D3D_DRIVER_TYPE_NULL:
214 FIXME("NULL device not implemented, falling back to refrast.\n");
215 /* fall through, for now */
216 case D3D_DRIVER_TYPE_REFERENCE:
218 HMODULE refrast = LoadLibraryA("d3d11ref.dll");
219 if (!refrast)
221 WARN("Failed to load refrast, returning E_FAIL.\n");
222 IDXGIFactory_Release(factory);
223 return E_FAIL;
225 hr = IDXGIFactory_CreateSoftwareAdapter(factory, refrast, &adapter);
226 FreeLibrary(refrast);
227 if (FAILED(hr))
229 WARN("Failed to create a software adapter, returning %#x.\n", hr);
230 IDXGIFactory_Release(factory);
231 return hr;
233 break;
236 case D3D_DRIVER_TYPE_SOFTWARE:
238 if (!swrast)
240 WARN("Software device requested, but NULL swrast passed, returning E_FAIL.\n");
241 IDXGIFactory_Release(factory);
242 return E_FAIL;
244 hr = IDXGIFactory_CreateSoftwareAdapter(factory, swrast, &adapter);
245 if (FAILED(hr))
247 WARN("Failed to create a software adapter, returning %#x.\n", hr);
248 IDXGIFactory_Release(factory);
249 return hr;
251 break;
254 default:
255 FIXME("Unhandled driver type %#x.\n", driver_type);
256 IDXGIFactory_Release(factory);
257 return E_FAIL;
261 hr = D3D11CoreCreateDevice(factory, adapter, flags, feature_levels, levels, &device);
262 IDXGIAdapter_Release(adapter);
263 IDXGIFactory_Release(factory);
264 if (FAILED(hr))
266 WARN("Failed to create a device, returning %#x.\n", hr);
267 return hr;
270 TRACE("Created ID3D11Device %p.\n", device);
272 if (obtained_feature_level)
273 *obtained_feature_level = ID3D11Device_GetFeatureLevel(device);
275 if (immediate_context)
276 ID3D11Device_GetImmediateContext(device, immediate_context);
278 if (device_out)
279 *device_out = device;
280 else
281 ID3D11Device_Release(device);
283 return S_OK;
286 HRESULT WINAPI D3D11CreateDeviceAndSwapChain(IDXGIAdapter *adapter, D3D_DRIVER_TYPE driver_type,
287 HMODULE swrast, UINT flags, const D3D_FEATURE_LEVEL *feature_levels, UINT levels,
288 UINT sdk_version, const DXGI_SWAP_CHAIN_DESC *swapchain_desc, IDXGISwapChain **swapchain,
289 ID3D11Device **device_out, D3D_FEATURE_LEVEL *obtained_feature_level, ID3D11DeviceContext **immediate_context)
291 DXGI_SWAP_CHAIN_DESC desc;
292 IDXGIDevice *dxgi_device;
293 IDXGIFactory *factory;
294 ID3D11Device *device;
295 HRESULT hr;
297 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
298 "swapchain_desc %p, swapchain %p, device %p, obtained_feature_level %p, immediate_context %p.\n",
299 adapter, debug_d3d_driver_type(driver_type), swrast, flags, feature_levels, levels, sdk_version,
300 swapchain_desc, swapchain, device_out, obtained_feature_level, immediate_context);
302 if (swapchain)
303 *swapchain = NULL;
304 if (device_out)
305 *device_out = NULL;
307 if (FAILED(hr = D3D11CreateDevice(adapter, driver_type, swrast, flags, feature_levels, levels, sdk_version,
308 &device, obtained_feature_level, immediate_context)))
310 WARN("Failed to create a device, returning %#x.\n", hr);
311 return hr;
314 if (swapchain)
316 if (FAILED(hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (void **)&dxgi_device)))
318 ERR("Failed to get a dxgi device from the d3d11 device, returning %#x.\n", hr);
319 goto cleanup;
322 hr = IDXGIDevice_GetAdapter(dxgi_device, &adapter);
323 IDXGIDevice_Release(dxgi_device);
324 if (FAILED(hr))
326 ERR("Failed to get the device adapter, returning %#x.\n", hr);
327 goto cleanup;
330 hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
331 IDXGIAdapter_Release(adapter);
332 if (FAILED(hr))
334 ERR("Failed to get the adapter factory, returning %#x.\n", hr);
335 goto cleanup;
338 desc = *swapchain_desc;
339 hr = IDXGIFactory_CreateSwapChain(factory, (IUnknown *)device, &desc, swapchain);
340 IDXGIFactory_Release(factory);
341 if (FAILED(hr))
343 WARN("Failed to create a swapchain, returning %#x.\n", hr);
344 goto cleanup;
347 TRACE("Created IDXGISwapChain %p.\n", *swapchain);
350 if (device_out)
351 *device_out = device;
352 else
353 ID3D11Device_Release(device);
355 return S_OK;
357 cleanup:
358 ID3D11Device_Release(device);
359 if (obtained_feature_level)
360 *obtained_feature_level = 0;
361 if (immediate_context)
363 ID3D11DeviceContext_Release(*immediate_context);
364 *immediate_context = NULL;
367 return hr;