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
;
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
);
91 if (FAILED(hr
= d3d_device_init(object
, device_object
)))
93 WARN("Failed to initialize device, hr %#x.\n", hr
);
97 *device_layer
= &object
->IUnknown_inner
;
99 TRACE("Created d3d10 device at %p\n", object
);
104 static void WINAPI
layer_set_feature_level(enum dxgi_device_layer_id id
, void *device
,
105 D3D_FEATURE_LEVEL feature_level
)
107 struct d3d_device
*d3d_device
= device
;
109 TRACE("id %#x, device %p, feature_level %u.\n", id
, device
, feature_level
);
111 if (id
!= DXGI_DEVICE_LAYER_D3D10_DEVICE
)
113 WARN("Unknown layer id %#x.\n", id
);
117 d3d_device
->feature_level
= feature_level
;
120 HRESULT WINAPI
D3D11CoreRegisterLayers(void)
122 static const struct dxgi_device_layer layers
[] =
124 {DXGI_DEVICE_LAYER_D3D10_DEVICE
, layer_init
, layer_get_size
, layer_create
, layer_set_feature_level
},
127 DXGID3D10RegisterLayers(layers
, sizeof(layers
)/sizeof(*layers
));
132 HRESULT WINAPI
D3D11CoreCreateDevice(IDXGIFactory
*factory
, IDXGIAdapter
*adapter
, UINT flags
,
133 const D3D_FEATURE_LEVEL
*feature_levels
, UINT levels
, ID3D11Device
**device
)
135 IUnknown
*dxgi_device
;
139 TRACE("factory %p, adapter %p, flags %#x, feature_levels %p, levels %u, device %p.\n",
140 factory
, adapter
, flags
, feature_levels
, levels
, device
);
142 d3d11
= GetModuleHandleA("d3d11.dll");
143 hr
= DXGID3D10CreateDevice(d3d11
, factory
, adapter
, flags
, feature_levels
, levels
, (void **)&dxgi_device
);
146 WARN("Failed to create device, returning %#x.\n", hr
);
150 hr
= IUnknown_QueryInterface(dxgi_device
, &IID_ID3D11Device
, (void **)device
);
151 IUnknown_Release(dxgi_device
);
154 ERR("Failed to query ID3D11Device interface, returning E_FAIL.\n");
161 HRESULT WINAPI
D3D11CreateDevice(IDXGIAdapter
*adapter
, D3D_DRIVER_TYPE driver_type
, HMODULE swrast
, UINT flags
,
162 const D3D_FEATURE_LEVEL
*feature_levels
, UINT levels
, UINT sdk_version
, ID3D11Device
**device_out
,
163 D3D_FEATURE_LEVEL
*obtained_feature_level
, ID3D11DeviceContext
**immediate_context
)
165 static const D3D_FEATURE_LEVEL default_feature_levels
[] =
167 D3D_FEATURE_LEVEL_11_0
,
168 D3D_FEATURE_LEVEL_10_1
,
169 D3D_FEATURE_LEVEL_10_0
,
170 D3D_FEATURE_LEVEL_9_3
,
171 D3D_FEATURE_LEVEL_9_2
,
172 D3D_FEATURE_LEVEL_9_1
,
174 IDXGIFactory
*factory
;
175 ID3D11Device
*device
;
178 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
179 "device %p, obtained_feature_level %p, immediate_context %p.\n",
180 adapter
, debug_d3d_driver_type(driver_type
), swrast
, flags
, feature_levels
, levels
, sdk_version
,
181 device_out
, obtained_feature_level
, immediate_context
);
185 if (obtained_feature_level
)
186 *obtained_feature_level
= 0;
187 if (immediate_context
)
188 *immediate_context
= NULL
;
192 IDXGIAdapter_AddRef(adapter
);
193 hr
= IDXGIAdapter_GetParent(adapter
, &IID_IDXGIFactory
, (void **)&factory
);
196 WARN("Failed to get dxgi factory, returning %#x.\n", hr
);
202 hr
= CreateDXGIFactory1(&IID_IDXGIFactory
, (void **)&factory
);
205 WARN("Failed to create dxgi factory, returning %#x.\n", hr
);
211 case D3D_DRIVER_TYPE_HARDWARE
:
213 hr
= IDXGIFactory_EnumAdapters(factory
, 0, &adapter
);
216 WARN("No adapters found, returning %#x.\n", hr
);
217 IDXGIFactory_Release(factory
);
223 case D3D_DRIVER_TYPE_NULL
:
224 FIXME("NULL device not implemented, falling back to refrast.\n");
225 /* fall through, for now */
226 case D3D_DRIVER_TYPE_REFERENCE
:
228 HMODULE refrast
= LoadLibraryA("d3d11ref.dll");
231 WARN("Failed to load refrast, returning E_FAIL.\n");
232 IDXGIFactory_Release(factory
);
235 hr
= IDXGIFactory_CreateSoftwareAdapter(factory
, refrast
, &adapter
);
236 FreeLibrary(refrast
);
239 WARN("Failed to create a software adapter, returning %#x.\n", hr
);
240 IDXGIFactory_Release(factory
);
246 case D3D_DRIVER_TYPE_SOFTWARE
:
250 WARN("Software device requested, but NULL swrast passed, returning E_FAIL.\n");
251 IDXGIFactory_Release(factory
);
254 hr
= IDXGIFactory_CreateSoftwareAdapter(factory
, swrast
, &adapter
);
257 WARN("Failed to create a software adapter, returning %#x.\n", hr
);
258 IDXGIFactory_Release(factory
);
265 FIXME("Unhandled driver type %#x.\n", driver_type
);
266 IDXGIFactory_Release(factory
);
273 feature_levels
= default_feature_levels
;
274 levels
= sizeof(default_feature_levels
) / sizeof(default_feature_levels
[0]);
276 hr
= D3D11CoreCreateDevice(factory
, adapter
, flags
, feature_levels
, levels
, &device
);
277 IDXGIAdapter_Release(adapter
);
278 IDXGIFactory_Release(factory
);
281 WARN("Failed to create a device, returning %#x.\n", hr
);
285 TRACE("Created ID3D11Device %p.\n", device
);
287 if (obtained_feature_level
)
288 *obtained_feature_level
= ID3D11Device_GetFeatureLevel(device
);
290 if (immediate_context
)
291 ID3D11Device_GetImmediateContext(device
, immediate_context
);
294 *device_out
= device
;
296 ID3D11Device_Release(device
);
301 HRESULT WINAPI
D3D11CreateDeviceAndSwapChain(IDXGIAdapter
*adapter
, D3D_DRIVER_TYPE driver_type
,
302 HMODULE swrast
, UINT flags
, const D3D_FEATURE_LEVEL
*feature_levels
, UINT levels
,
303 UINT sdk_version
, const DXGI_SWAP_CHAIN_DESC
*swapchain_desc
, IDXGISwapChain
**swapchain
,
304 ID3D11Device
**device_out
, D3D_FEATURE_LEVEL
*obtained_feature_level
, ID3D11DeviceContext
**immediate_context
)
306 DXGI_SWAP_CHAIN_DESC desc
;
307 IDXGIDevice
*dxgi_device
;
308 IDXGIFactory
*factory
;
309 ID3D11Device
*device
;
312 TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, feature_levels %p, levels %u, sdk_version %u, "
313 "swapchain_desc %p, swapchain %p, device %p, obtained_feature_level %p, immediate_context %p.\n",
314 adapter
, debug_d3d_driver_type(driver_type
), swrast
, flags
, feature_levels
, levels
, sdk_version
,
315 swapchain_desc
, swapchain
, device_out
, obtained_feature_level
, immediate_context
);
322 if (FAILED(hr
= D3D11CreateDevice(adapter
, driver_type
, swrast
, flags
, feature_levels
, levels
, sdk_version
,
323 &device
, obtained_feature_level
, immediate_context
)))
325 WARN("Failed to create a device, returning %#x.\n", hr
);
331 if (FAILED(hr
= ID3D11Device_QueryInterface(device
, &IID_IDXGIDevice
, (void **)&dxgi_device
)))
333 ERR("Failed to get a dxgi device from the d3d11 device, returning %#x.\n", hr
);
337 hr
= IDXGIDevice_GetAdapter(dxgi_device
, &adapter
);
338 IDXGIDevice_Release(dxgi_device
);
341 ERR("Failed to get the device adapter, returning %#x.\n", hr
);
345 hr
= IDXGIAdapter_GetParent(adapter
, &IID_IDXGIFactory
, (void **)&factory
);
346 IDXGIAdapter_Release(adapter
);
349 ERR("Failed to get the adapter factory, returning %#x.\n", hr
);
353 desc
= *swapchain_desc
;
354 hr
= IDXGIFactory_CreateSwapChain(factory
, (IUnknown
*)device
, &desc
, swapchain
);
355 IDXGIFactory_Release(factory
);
358 WARN("Failed to create a swapchain, returning %#x.\n", hr
);
362 TRACE("Created IDXGISwapChain %p.\n", *swapchain
);
366 *device_out
= device
;
368 ID3D11Device_Release(device
);
373 ID3D11Device_Release(device
);
374 if (obtained_feature_level
)
375 *obtained_feature_level
= 0;
376 if (immediate_context
)
378 ID3D11DeviceContext_Release(*immediate_context
);
379 *immediate_context
= NULL
;