Home | History | Annotate | Download | only in dxgid3d11
      1 /**************************************************************************
      2  *
      3  * Copyright 2010 Luca Barbieri
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "software"), to deal in the software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the software, and to
     10  * permit persons to whom the software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the
     14  * next paragraph) shall be included in all copies or substantial
     15  * portions of the software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     24  *
     25  **************************************************************************/
     26 
     27 #include "d3d1xstutil.h"
     28 #include "galliumd3d11.h"
     29 #include <dxgi.h>
     30 #include "pipe/p_screen.h"
     31 #include "pipe/p_context.h"
     32 
     33 HRESULT D3D11CreateDevice(
     34 	IDXGIAdapter *adapter,
     35 	D3D_DRIVER_TYPE driver_type,
     36 	HMODULE software,
     37 	unsigned flags,
     38 	const D3D_FEATURE_LEVEL *feature_levels,
     39 	unsigned num_feature_levels,
     40 	unsigned sdk_version,
     41 	ID3D11Device **out_device,
     42 	D3D_FEATURE_LEVEL *feature_level,
     43 	ID3D11DeviceContext **out_immediate_context
     44 )
     45 {
     46 	HRESULT hr;
     47 	ComPtr<IDXGIAdapter1> adapter_to_release;
     48 	if(!adapter)
     49 	{
     50 		ComPtr<IDXGIFactory1> factory;
     51 		hr = CreateDXGIFactory1(IID_IDXGIFactory1, (void**)&factory);
     52 		if(!SUCCEEDED(hr))
     53 			return hr;
     54 		hr = factory->EnumAdapters1(0, &adapter_to_release);
     55 		if(!SUCCEEDED(hr))
     56 			return hr;
     57 		adapter = adapter_to_release.p;
     58 	}
     59 	ComPtr<IGalliumAdapter> gallium_adapter;
     60 	hr = adapter->QueryInterface(IID_IGalliumAdapter, (void**)&gallium_adapter);
     61 	if(!SUCCEEDED(hr))
     62 		return hr;
     63 	struct pipe_screen* screen;
     64 	// TODO: what should D3D_DRIVER_TYPE_SOFTWARE return? fast or reference?
     65 	if(driver_type == D3D_DRIVER_TYPE_REFERENCE)
     66 		screen = gallium_adapter->GetGalliumReferenceSoftwareScreen();
     67 	else if(driver_type == D3D_DRIVER_TYPE_SOFTWARE || driver_type == D3D_DRIVER_TYPE_WARP)
     68 		screen = gallium_adapter->GetGalliumFastSoftwareScreen();
     69 	else
     70 		screen = gallium_adapter->GetGalliumScreen();
     71 	if(!screen)
     72 		return E_FAIL;
     73 	struct pipe_context* context = screen->context_create(screen, 0);
     74 	if(!context)
     75 		return E_FAIL;
     76 	ComPtr<ID3D11Device> device;
     77 	hr = GalliumD3D11DeviceCreate(screen, context, TRUE, flags, adapter, &device);
     78 	if(!SUCCEEDED(hr))
     79 	{
     80 		context->destroy(context);
     81 		return hr;
     82 	}
     83 	if(out_immediate_context)
     84 		device->GetImmediateContext(out_immediate_context);
     85 	if(feature_level)
     86 		*feature_level = device->GetFeatureLevel();
     87 	if(out_device)
     88 		*out_device = device.steal();
     89 	return S_OK;
     90 }
     91 
     92 HRESULT WINAPI D3D11CreateDeviceAndSwapChain(
     93 	IDXGIAdapter* adapter,
     94 	D3D_DRIVER_TYPE driver_type,
     95 	HMODULE software,
     96 	unsigned flags,
     97 	CONST D3D_FEATURE_LEVEL* feature_levels,
     98 	unsigned num_feature_levels,
     99 	unsigned sdk_version,
    100 	CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
    101 	IDXGISwapChain** out_swap_chain,
    102 	ID3D11Device** out_device,
    103 	D3D_FEATURE_LEVEL* feature_level,
    104 	ID3D11DeviceContext** out_immediate_context )
    105 {
    106 	ComPtr<ID3D11Device> dev;
    107 	ComPtr<ID3D11DeviceContext> ctx;
    108 	HRESULT hr;
    109 	hr = D3D11CreateDevice(adapter, driver_type, software, flags, feature_levels, num_feature_levels, sdk_version, (ID3D11Device**)&dev, feature_level, (ID3D11DeviceContext**)&ctx);
    110 	if(!SUCCEEDED(hr))
    111 		return hr;
    112 	if(out_swap_chain)
    113 	{
    114 		ComPtr<IDXGIFactory> factory;
    115 		ComPtr<IDXGIDevice> dxgi_device;
    116 		ComPtr<IDXGIAdapter> adapter;
    117 		hr = dev->QueryInterface(IID_IDXGIDevice, (void**)&dxgi_device);
    118 		if(!SUCCEEDED(hr))
    119 			return hr;
    120 
    121 		hr = dxgi_device->GetAdapter(&adapter);
    122 		if(!SUCCEEDED(hr))
    123 			return hr;
    124 
    125 		adapter->GetParent(IID_IDXGIFactory, (void**)&factory);
    126 		hr = factory->CreateSwapChain(dev.p, (DXGI_SWAP_CHAIN_DESC*)pSwapChainDesc, out_swap_chain);
    127 		if(!SUCCEEDED(hr))
    128 			return hr;
    129 	}
    130 	if(out_device)
    131 		*out_device = dev.steal();
    132 	if(out_immediate_context)
    133 		*out_immediate_context = ctx.steal();
    134 	return hr;
    135 }
    136