d3dx8: Implement D3DXMatrixStack_Scale.
[wine] / dlls / d3d10 / d3d10_main.c
1 /*
2  * Direct3D 10
3  *
4  * Copyright 2007 Andras Kovacs
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include "d3d10_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d10);
28
29 /* At process attach */
30 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
31 {
32     TRACE("fdwReason=%d\n", fdwReason);
33     switch(fdwReason)
34     {
35     case DLL_PROCESS_ATTACH:
36         DisableThreadLibraryCalls( hInstDLL );
37         break;
38     }
39     return TRUE;
40 }
41
42 HRESULT WINAPI D3D10CreateDevice(IDXGIAdapter *adapter, D3D10_DRIVER_TYPE driver_type,
43         HMODULE swrast, UINT flags, UINT sdk_version, ID3D10Device **device)
44 {
45     IDXGIFactory *factory;
46     HRESULT hr;
47
48     TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, sdk_version %d, device %p\n",
49             adapter, debug_d3d10_driver_type(driver_type), swrast, flags, sdk_version, device);
50
51     if (adapter)
52     {
53         IDXGIAdapter_AddRef(adapter);
54         hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
55         if (FAILED(hr))
56         {
57             WARN("Failed to get dxgi factory, returning %#x\n", hr);
58             return hr;
59         }
60     }
61     else
62     {
63         hr = CreateDXGIFactory(&IID_IDXGIFactory, (void **)&factory);
64         if (FAILED(hr))
65         {
66             WARN("Failed to create dxgi factory, returning %#x\n", hr);
67             return hr;
68         }
69
70         switch(driver_type)
71         {
72             case D3D10_DRIVER_TYPE_HARDWARE:
73             {
74                 hr = IDXGIFactory_EnumAdapters(factory, 0, &adapter);
75                 if (FAILED(hr))
76                 {
77                     WARN("No adapters found, returning %#x\n", hr);
78                     IDXGIFactory_Release(factory);
79                     return hr;
80                 }
81                 break;
82             }
83
84             case D3D10_DRIVER_TYPE_NULL:
85                 FIXME("NULL device not implemented, falling back to refrast\n");
86             case D3D10_DRIVER_TYPE_REFERENCE:
87             {
88                 HMODULE refrast = LoadLibraryA("d3d10ref.dll");
89                 if (!refrast)
90                 {
91                     WARN("Failed to load refrast, returning E_FAIL\n");
92                     IDXGIFactory_Release(factory);
93                     return E_FAIL;
94                 }
95                 hr = IDXGIFactory_CreateSoftwareAdapter(factory, refrast, &adapter);
96                 FreeLibrary(refrast);
97                 if (FAILED(hr))
98                 {
99                     WARN("Failed to create a software adapter, returning %#x\n", hr);
100                     IDXGIFactory_Release(factory);
101                     return hr;
102                 }
103                 break;
104             }
105
106             case D3D10_DRIVER_TYPE_SOFTWARE:
107             {
108                 if (!swrast)
109                 {
110                     WARN("Software device requested, but NULL swrast passed, returning E_FAIL\n");
111                     IDXGIFactory_Release(factory);
112                     return E_FAIL;
113                 }
114                 hr = IDXGIFactory_CreateSoftwareAdapter(factory, swrast, &adapter);
115                 if (FAILED(hr))
116                 {
117                     WARN("Failed to create a software adapter, returning %#x\n", hr);
118                     IDXGIFactory_Release(factory);
119                     return hr;
120                 }
121                 break;
122             }
123         }
124     }
125
126     hr = D3D10CoreCreateDevice(factory, adapter, flags, 0, device);
127     IDXGIAdapter_Release(adapter);
128     IDXGIFactory_Release(factory);
129     if (FAILED(hr))
130     {
131         WARN("Failed to create a device, returning %#x\n", hr);
132         return hr;
133     }
134
135     TRACE("Created ID3D10Device %p\n", *device);
136
137     return hr;
138 }
139
140 HRESULT WINAPI D3D10CreateDeviceAndSwapChain(IDXGIAdapter *adapter, D3D10_DRIVER_TYPE driver_type,
141         HMODULE swrast, UINT flags, UINT sdk_version, DXGI_SWAP_CHAIN_DESC *swapchain_desc,
142         IDXGISwapChain **swapchain, ID3D10Device **device)
143 {
144     IDXGIDevice *dxgi_device;
145     IDXGIFactory *factory;
146     HRESULT hr;
147
148     TRACE("adapter %p, driver_type %s, swrast %p, flags %#x, sdk_version %d,\n"
149             "\tswapchain_desc %p, swapchain %p, device %p\n",
150             adapter, debug_d3d10_driver_type(driver_type), swrast, flags, sdk_version,
151             swapchain_desc, swapchain, device);
152
153     hr = D3D10CreateDevice(adapter, driver_type, swrast, flags, sdk_version, device);
154     if (FAILED(hr))
155     {
156         WARN("Failed to create a device, returning %#x\n", hr);
157         *device = NULL;
158         return hr;
159     }
160
161     TRACE("Created ID3D10Device %p\n", *device);
162
163     hr = ID3D10Device_QueryInterface(*device, &IID_IDXGIDevice, (void **)&dxgi_device);
164     if (FAILED(hr))
165     {
166         ERR("Failed to get a dxgi device from the d3d10 device, returning %#x\n", hr);
167         ID3D10Device_Release(*device);
168         *device = NULL;
169         return hr;
170     }
171
172     hr = IDXGIDevice_GetAdapter(dxgi_device, &adapter);
173     IDXGIDevice_Release(dxgi_device);
174     if (FAILED(hr))
175     {
176         ERR("Failed to get the device adapter, returning %#x\n", hr);
177         ID3D10Device_Release(*device);
178         *device = NULL;
179         return hr;
180     }
181
182     hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
183     IDXGIAdapter_Release(adapter);
184     if (FAILED(hr))
185     {
186         ERR("Failed to get the adapter factory, returning %#x\n", hr);
187         ID3D10Device_Release(*device);
188         *device = NULL;
189         return hr;
190     }
191
192     hr = IDXGIFactory_CreateSwapChain(factory, (IUnknown *)*device, swapchain_desc, swapchain);
193     IDXGIFactory_Release(factory);
194     if (FAILED(hr))
195     {
196         ID3D10Device_Release(*device);
197         *device = NULL;
198
199         WARN("Failed to create a swapchain, returning %#x\n", hr);
200         return hr;
201     }
202
203     TRACE("Created IDXGISwapChain %p\n", *swapchain);
204
205     return S_OK;
206 }