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 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
);
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
));
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
;
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
);
145 WARN("Failed to create device, returning %#x.\n", hr
);
149 hr
= IUnknown_QueryInterface(dxgi_device
, &IID_ID3D11Device
, (void **)device
);
150 IUnknown_Release(dxgi_device
);
153 ERR("Failed to query ID3D11Device interface, returning E_FAIL.\n");
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
;
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
);
175 if (obtained_feature_level
)
176 *obtained_feature_level
= 0;
177 if (immediate_context
)
178 *immediate_context
= NULL
;
182 IDXGIAdapter_AddRef(adapter
);
183 hr
= IDXGIAdapter_GetParent(adapter
, &IID_IDXGIFactory
, (void **)&factory
);
186 WARN("Failed to get dxgi factory, returning %#x.\n", hr
);
192 hr
= CreateDXGIFactory1(&IID_IDXGIFactory
, (void **)&factory
);
195 WARN("Failed to create dxgi factory, returning %#x.\n", hr
);
201 case D3D_DRIVER_TYPE_HARDWARE
:
203 hr
= IDXGIFactory_EnumAdapters(factory
, 0, &adapter
);
206 WARN("No adapters found, returning %#x.\n", hr
);
207 IDXGIFactory_Release(factory
);
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");
221 WARN("Failed to load refrast, returning E_FAIL.\n");
222 IDXGIFactory_Release(factory
);
225 hr
= IDXGIFactory_CreateSoftwareAdapter(factory
, refrast
, &adapter
);
226 FreeLibrary(refrast
);
229 WARN("Failed to create a software adapter, returning %#x.\n", hr
);
230 IDXGIFactory_Release(factory
);
236 case D3D_DRIVER_TYPE_SOFTWARE
:
240 WARN("Software device requested, but NULL swrast passed, returning E_FAIL.\n");
241 IDXGIFactory_Release(factory
);
244 hr
= IDXGIFactory_CreateSoftwareAdapter(factory
, swrast
, &adapter
);
247 WARN("Failed to create a software adapter, returning %#x.\n", hr
);
248 IDXGIFactory_Release(factory
);
255 FIXME("Unhandled driver type %#x.\n", driver_type
);
256 IDXGIFactory_Release(factory
);
261 hr
= D3D11CoreCreateDevice(factory
, adapter
, flags
, feature_levels
, levels
, &device
);
262 IDXGIAdapter_Release(adapter
);
263 IDXGIFactory_Release(factory
);
266 WARN("Failed to create a device, returning %#x.\n", 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
);
279 *device_out
= device
;
281 ID3D11Device_Release(device
);
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
;
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
);
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
);
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
);
322 hr
= IDXGIDevice_GetAdapter(dxgi_device
, &adapter
);
323 IDXGIDevice_Release(dxgi_device
);
326 ERR("Failed to get the device adapter, returning %#x.\n", hr
);
330 hr
= IDXGIAdapter_GetParent(adapter
, &IID_IDXGIFactory
, (void **)&factory
);
331 IDXGIAdapter_Release(adapter
);
334 ERR("Failed to get the adapter factory, returning %#x.\n", hr
);
338 desc
= *swapchain_desc
;
339 hr
= IDXGIFactory_CreateSwapChain(factory
, (IUnknown
*)device
, &desc
, swapchain
);
340 IDXGIFactory_Release(factory
);
343 WARN("Failed to create a swapchain, returning %#x.\n", hr
);
347 TRACE("Created IDXGISwapChain %p.\n", *swapchain
);
351 *device_out
= device
;
353 ID3D11Device_Release(device
);
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
;