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