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
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
)
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
);
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
);
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
);
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
;
79 TRACE("id %#x, layer_base %p, unknown0 %#x, device_object %p, riid %s, device_layer %p\n",
80 id
, layer_base
, unknown0
, device_object
, debugstr_guid(riid
), device_layer
);
82 if (id
!= DXGI_DEVICE_LAYER_D3D10_DEVICE
)
84 WARN("Unknown layer id %#x\n", id
);
90 d3d_device_init(object
, device_object
);
91 *device_layer
= &object
->IUnknown_inner
;
93 TRACE("Created d3d10 device at %p\n", object
);
98 static void WINAPI
layer_set_feature_level(enum dxgi_device_layer_id id
, void *device
,
99 D3D_FEATURE_LEVEL feature_level
)
101 struct d3d_device
*d3d_device
= device
;
103 TRACE("id %#x, device %p, feature_level %#x.\n", id
, device
, feature_level
);
105 if (id
!= DXGI_DEVICE_LAYER_D3D10_DEVICE
)
107 WARN("Unknown layer id %#x.\n", id
);
111 d3d_device
->feature_level
= feature_level
;
114 HRESULT WINAPI
D3D11CoreRegisterLayers(void)
116 static const struct dxgi_device_layer layers
[] =
118 {DXGI_DEVICE_LAYER_D3D10_DEVICE
, layer_init
, layer_get_size
, layer_create
, layer_set_feature_level
},
121 DXGID3D10RegisterLayers(layers
, sizeof(layers
)/sizeof(*layers
));
126 HRESULT WINAPI
D3D11CoreCreateDevice(IDXGIFactory
*factory
, IDXGIAdapter
*adapter
, UINT flags
,
127 const D3D_FEATURE_LEVEL
*feature_levels
, UINT levels
, ID3D11Device
**device
)
129 IUnknown
*dxgi_device
;
133 TRACE("factory %p, adapter %p, flags %#x, feature_levels %p, levels %u, device %p.\n",
134 factory
, adapter
, flags
, feature_levels
, levels
, device
);
136 d3d11
= GetModuleHandleA("d3d11.dll");
137 hr
= DXGID3D10CreateDevice(d3d11
, factory
, adapter
, flags
, feature_levels
, levels
, (void **)&dxgi_device
);
140 WARN("Failed to create device, returning %#x.\n", hr
);
144 hr
= IUnknown_QueryInterface(dxgi_device
, &IID_ID3D11Device
, (void **)device
);
145 IUnknown_Release(dxgi_device
);
148 ERR("Failed to query ID3D11Device interface, returning E_FAIL.\n");
155 HRESULT WINAPI
D3D11CreateDevice(IDXGIAdapter
*adapter
, D3D_DRIVER_TYPE driver_type
, HMODULE swrast
, UINT flags
,
156 const D3D_FEATURE_LEVEL
*feature_levels
, UINT levels
, UINT sdk_version
, ID3D11Device
**device_out
,
157 D3D_FEATURE_LEVEL
*obtained_feature_level
, ID3D11DeviceContext
**immediate_context
)
159 static const D3D_FEATURE_LEVEL default_feature_levels
[] =
161 D3D_FEATURE_LEVEL_11_0
,
162 D3D_FEATURE_LEVEL_10_1
,
163 D3D_FEATURE_LEVEL_10_0
,
164 D3D_FEATURE_LEVEL_9_3
,
165 D3D_FEATURE_LEVEL_9_2
,
166 D3D_FEATURE_LEVEL_9_1
,
168 IDXGIFactory
*factory
;
169 ID3D11Device
*device
;
172 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
173 "device %p, obtained_feature_level %p, immediate_context %p.\n",
174 adapter
, debug_d3d_driver_type(driver_type
), swrast
, flags
, feature_levels
, levels
, sdk_version
,
175 device_out
, obtained_feature_level
, immediate_context
);
179 if (obtained_feature_level
)
180 *obtained_feature_level
= 0;
181 if (immediate_context
)
182 *immediate_context
= NULL
;
186 IDXGIAdapter_AddRef(adapter
);
187 hr
= IDXGIAdapter_GetParent(adapter
, &IID_IDXGIFactory
, (void **)&factory
);
190 WARN("Failed to get dxgi factory, returning %#x.\n", hr
);
196 hr
= CreateDXGIFactory1(&IID_IDXGIFactory
, (void **)&factory
);
199 WARN("Failed to create dxgi factory, returning %#x.\n", hr
);
205 case D3D_DRIVER_TYPE_HARDWARE
:
207 hr
= IDXGIFactory_EnumAdapters(factory
, 0, &adapter
);
210 WARN("No adapters found, returning %#x.\n", hr
);
211 IDXGIFactory_Release(factory
);
217 case D3D_DRIVER_TYPE_NULL
:
218 FIXME("NULL device not implemented, falling back to refrast.\n");
219 /* fall through, for now */
220 case D3D_DRIVER_TYPE_REFERENCE
:
222 HMODULE refrast
= LoadLibraryA("d3d11ref.dll");
225 WARN("Failed to load refrast, returning E_FAIL.\n");
226 IDXGIFactory_Release(factory
);
229 hr
= IDXGIFactory_CreateSoftwareAdapter(factory
, refrast
, &adapter
);
230 FreeLibrary(refrast
);
233 WARN("Failed to create a software adapter, returning %#x.\n", hr
);
234 IDXGIFactory_Release(factory
);
240 case D3D_DRIVER_TYPE_SOFTWARE
:
244 WARN("Software device requested, but NULL swrast passed, returning E_FAIL.\n");
245 IDXGIFactory_Release(factory
);
248 hr
= IDXGIFactory_CreateSoftwareAdapter(factory
, swrast
, &adapter
);
251 WARN("Failed to create a software adapter, returning %#x.\n", hr
);
252 IDXGIFactory_Release(factory
);
259 FIXME("Unhandled driver type %#x.\n", driver_type
);
260 IDXGIFactory_Release(factory
);
267 feature_levels
= default_feature_levels
;
268 levels
= sizeof(default_feature_levels
) / sizeof(default_feature_levels
[0]);
270 hr
= D3D11CoreCreateDevice(factory
, adapter
, flags
, feature_levels
, levels
, &device
);
271 IDXGIAdapter_Release(adapter
);
272 IDXGIFactory_Release(factory
);
275 WARN("Failed to create a device, returning %#x.\n", hr
);
279 TRACE("Created ID3D11Device %p.\n", device
);
281 if (obtained_feature_level
)
282 *obtained_feature_level
= ID3D11Device_GetFeatureLevel(device
);
284 if (immediate_context
)
285 ID3D11Device_GetImmediateContext(device
, immediate_context
);
288 *device_out
= device
;
290 ID3D11Device_Release(device
);
292 return (device_out
|| immediate_context
) ? S_OK
: S_FALSE
;
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
;
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
);
316 if (FAILED(hr
= D3D11CreateDevice(adapter
, driver_type
, swrast
, flags
, feature_levels
, levels
, sdk_version
,
317 &device
, obtained_feature_level
, immediate_context
)))
319 WARN("Failed to create a device, returning %#x.\n", hr
);
325 if (FAILED(hr
= ID3D11Device_QueryInterface(device
, &IID_IDXGIDevice
, (void **)&dxgi_device
)))
327 ERR("Failed to get a dxgi device from the d3d11 device, returning %#x.\n", hr
);
331 hr
= IDXGIDevice_GetAdapter(dxgi_device
, &adapter
);
332 IDXGIDevice_Release(dxgi_device
);
335 ERR("Failed to get the device adapter, returning %#x.\n", hr
);
339 hr
= IDXGIAdapter_GetParent(adapter
, &IID_IDXGIFactory
, (void **)&factory
);
340 IDXGIAdapter_Release(adapter
);
343 ERR("Failed to get the adapter factory, returning %#x.\n", hr
);
347 desc
= *swapchain_desc
;
348 hr
= IDXGIFactory_CreateSwapChain(factory
, (IUnknown
*)device
, &desc
, swapchain
);
349 IDXGIFactory_Release(factory
);
352 WARN("Failed to create a swapchain, returning %#x.\n", hr
);
356 TRACE("Created IDXGISwapChain %p.\n", *swapchain
);
360 *device_out
= device
;
362 ID3D11Device_Release(device
);
364 return (swapchain
|| device_out
|| immediate_context
) ? S_OK
: S_FALSE
;
367 ID3D11Device_Release(device
);
368 if (obtained_feature_level
)
369 *obtained_feature_level
= 0;
370 if (immediate_context
)
372 ID3D11DeviceContext_Release(*immediate_context
);
373 *immediate_context
= NULL
;