d3d9: Implement d3d9_GetAdapterModeCountEx().
[wine] / dlls / d3d8 / directx.c
1 /*
2  * IDirect3D8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 #include "d3d8_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39
40 static inline struct d3d8 *impl_from_IDirect3D8(IDirect3D8 *iface)
41 {
42     return CONTAINING_RECORD(iface, struct d3d8, IDirect3D8_iface);
43 }
44
45 static HRESULT WINAPI d3d8_QueryInterface(IDirect3D8 *iface, REFIID riid, void **out)
46 {
47     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
48
49     if (IsEqualGUID(riid, &IID_IDirect3D8)
50             || IsEqualGUID(riid, &IID_IUnknown))
51     {
52         IUnknown_AddRef(iface);
53         *out = iface;
54         return S_OK;
55     }
56
57     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
58
59     *out = NULL;
60     return E_NOINTERFACE;
61 }
62
63 static ULONG WINAPI d3d8_AddRef(IDirect3D8 *iface)
64 {
65     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
66     ULONG refcount = InterlockedIncrement(&d3d8->refcount);
67
68     TRACE("%p increasing refcount to %u.\n", iface, refcount);
69
70     return refcount;
71 }
72
73 static ULONG WINAPI d3d8_Release(IDirect3D8 *iface)
74 {
75     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
76     ULONG refcount = InterlockedDecrement(&d3d8->refcount);
77
78     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
79
80     if (!refcount)
81     {
82         wined3d_mutex_lock();
83         wined3d_decref(d3d8->wined3d);
84         wined3d_mutex_unlock();
85
86         HeapFree(GetProcessHeap(), 0, d3d8);
87     }
88
89     return refcount;
90 }
91
92 static HRESULT WINAPI d3d8_RegisterSoftwareDevice(IDirect3D8 *iface, void *init_function)
93 {
94     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
95     HRESULT hr;
96
97     TRACE("iface %p, init_function %p.\n", iface, init_function);
98
99     wined3d_mutex_lock();
100     hr = wined3d_register_software_device(d3d8->wined3d, init_function);
101     wined3d_mutex_unlock();
102
103     return hr;
104 }
105
106 static UINT WINAPI d3d8_GetAdapterCount(IDirect3D8 *iface)
107 {
108     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
109     HRESULT hr;
110
111     TRACE("iface %p.\n", iface);
112
113     wined3d_mutex_lock();
114     hr = wined3d_get_adapter_count(d3d8->wined3d);
115     wined3d_mutex_unlock();
116
117     return hr;
118 }
119
120 static HRESULT WINAPI d3d8_GetAdapterIdentifier(IDirect3D8 *iface, UINT adapter,
121         DWORD flags, D3DADAPTER_IDENTIFIER8 *identifier)
122 {
123     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
124     struct wined3d_adapter_identifier adapter_id;
125     HRESULT hr;
126
127     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
128             iface, adapter, flags, identifier);
129
130     adapter_id.driver = identifier->Driver;
131     adapter_id.driver_size = sizeof(identifier->Driver);
132     adapter_id.description = identifier->Description;
133     adapter_id.description_size = sizeof(identifier->Description);
134     adapter_id.device_name = NULL; /* d3d9 only */
135     adapter_id.device_name_size = 0; /* d3d9 only */
136
137     wined3d_mutex_lock();
138     hr = wined3d_get_adapter_identifier(d3d8->wined3d, adapter, flags, &adapter_id);
139     wined3d_mutex_unlock();
140
141     identifier->DriverVersion = adapter_id.driver_version;
142     identifier->VendorId = adapter_id.vendor_id;
143     identifier->DeviceId = adapter_id.device_id;
144     identifier->SubSysId = adapter_id.subsystem_id;
145     identifier->Revision = adapter_id.revision;
146     memcpy(&identifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(identifier->DeviceIdentifier));
147     identifier->WHQLLevel = adapter_id.whql_level;
148
149     return hr;
150 }
151
152 static UINT WINAPI d3d8_GetAdapterModeCount(IDirect3D8 *iface, UINT adapter)
153 {
154     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
155     HRESULT hr;
156
157     TRACE("iface %p, adapter %u.\n", iface, adapter);
158
159     wined3d_mutex_lock();
160     hr = wined3d_get_adapter_mode_count(d3d8->wined3d, adapter,
161             WINED3DFMT_UNKNOWN, WINED3D_SCANLINE_ORDERING_UNKNOWN);
162     wined3d_mutex_unlock();
163
164     return hr;
165 }
166
167 static HRESULT WINAPI d3d8_EnumAdapterModes(IDirect3D8 *iface, UINT adapter, UINT mode_idx, D3DDISPLAYMODE *mode)
168 {
169     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
170     struct wined3d_display_mode wined3d_mode;
171     HRESULT hr;
172
173     TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
174             iface, adapter, mode_idx, mode);
175
176     wined3d_mutex_lock();
177     hr = wined3d_enum_adapter_modes(d3d8->wined3d, adapter, WINED3DFMT_UNKNOWN, mode_idx, &wined3d_mode);
178     wined3d_mutex_unlock();
179
180     if (SUCCEEDED(hr))
181     {
182         mode->Width = wined3d_mode.width;
183         mode->Height = wined3d_mode.height;
184         mode->RefreshRate = wined3d_mode.refresh_rate;
185         mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
186     }
187
188     return hr;
189 }
190
191 static HRESULT WINAPI d3d8_GetAdapterDisplayMode(IDirect3D8 *iface, UINT adapter, D3DDISPLAYMODE *mode)
192 {
193     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
194     struct wined3d_display_mode wined3d_mode;
195     HRESULT hr;
196
197     TRACE("iface %p, adapter %u, mode %p.\n",
198             iface, adapter, mode);
199
200     wined3d_mutex_lock();
201     hr = wined3d_get_adapter_display_mode(d3d8->wined3d, adapter, &wined3d_mode, NULL);
202     wined3d_mutex_unlock();
203
204     if (SUCCEEDED(hr))
205     {
206         mode->Width = wined3d_mode.width;
207         mode->Height = wined3d_mode.height;
208         mode->RefreshRate = wined3d_mode.refresh_rate;
209         mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
210     }
211
212     return hr;
213 }
214
215 static HRESULT WINAPI d3d8_CheckDeviceType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
216         D3DFORMAT display_format, D3DFORMAT backbuffer_format, BOOL windowed)
217 {
218     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
219     HRESULT hr;
220
221     TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
222             iface, adapter, device_type, display_format, backbuffer_format, windowed);
223
224     wined3d_mutex_lock();
225     hr = wined3d_check_device_type(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(display_format),
226             wined3dformat_from_d3dformat(backbuffer_format), windowed);
227     wined3d_mutex_unlock();
228
229     return hr;
230 }
231
232 static HRESULT WINAPI d3d8_CheckDeviceFormat(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
233         D3DFORMAT adapter_format, DWORD usage, D3DRESOURCETYPE resource_type, D3DFORMAT format)
234 {
235     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
236     enum wined3d_resource_type wined3d_rtype;
237     HRESULT hr;
238
239     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
240             iface, adapter, device_type, adapter_format, usage, resource_type, format);
241
242     switch (resource_type)
243     {
244         case D3DRTYPE_VERTEXBUFFER:
245         case D3DRTYPE_INDEXBUFFER:
246             wined3d_rtype = WINED3D_RTYPE_BUFFER;
247             break;
248
249         default:
250             wined3d_rtype = resource_type;
251             break;
252     }
253
254     wined3d_mutex_lock();
255     hr = wined3d_check_device_format(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(adapter_format),
256             usage, wined3d_rtype, wined3dformat_from_d3dformat(format), WINED3D_SURFACE_TYPE_OPENGL);
257     wined3d_mutex_unlock();
258
259     return hr;
260 }
261
262 static HRESULT WINAPI d3d8_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
263         D3DFORMAT format, BOOL windowed, D3DMULTISAMPLE_TYPE multisample_type)
264 {
265     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
266     HRESULT hr;
267
268     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
269             iface, adapter, device_type, format, windowed, multisample_type);
270
271     wined3d_mutex_lock();
272     hr = wined3d_check_device_multisample_type(d3d8->wined3d, adapter, device_type,
273             wined3dformat_from_d3dformat(format), windowed,
274             (enum wined3d_multisample_type)multisample_type, NULL);
275     wined3d_mutex_unlock();
276
277     return hr;
278 }
279
280 static HRESULT WINAPI d3d8_CheckDepthStencilMatch(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
281         D3DFORMAT adapter_format, D3DFORMAT rt_format, D3DFORMAT ds_format)
282 {
283     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
284     HRESULT hr;
285
286     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
287             iface, adapter, device_type, adapter_format, rt_format, ds_format);
288
289     wined3d_mutex_lock();
290     hr = wined3d_check_depth_stencil_match(d3d8->wined3d, adapter, device_type,
291             wined3dformat_from_d3dformat(adapter_format), wined3dformat_from_d3dformat(rt_format),
292             wined3dformat_from_d3dformat(ds_format));
293     wined3d_mutex_unlock();
294
295     return hr;
296 }
297
298 void fixup_caps(WINED3DCAPS *caps)
299 {
300     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
301     if (caps->PixelShaderVersion)
302         caps->PixelShaderVersion = D3DPS_VERSION(1,4);
303     else
304         caps->PixelShaderVersion = D3DPS_VERSION(0,0);
305     if (caps->VertexShaderVersion)
306         caps->VertexShaderVersion = D3DVS_VERSION(1,1);
307     else
308         caps->VertexShaderVersion = D3DVS_VERSION(0,0);
309     caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
310
311     caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
312 }
313
314 static HRESULT WINAPI d3d8_GetDeviceCaps(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type, D3DCAPS8 *caps)
315 {
316     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
317     WINED3DCAPS *wined3d_caps;
318     HRESULT hr;
319
320     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, adapter, device_type, caps);
321
322     if (!caps)
323         return D3DERR_INVALIDCALL;
324
325     if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
326         return D3DERR_INVALIDCALL;
327
328     wined3d_mutex_lock();
329     hr = wined3d_get_device_caps(d3d8->wined3d, adapter, device_type, wined3d_caps);
330     wined3d_mutex_unlock();
331
332     fixup_caps(wined3d_caps);
333     WINECAPSTOD3D8CAPS(caps, wined3d_caps)
334     HeapFree(GetProcessHeap(), 0, wined3d_caps);
335
336     return hr;
337 }
338
339 static HMONITOR WINAPI d3d8_GetAdapterMonitor(IDirect3D8 *iface, UINT adapter)
340 {
341     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
342     HMONITOR ret;
343
344     TRACE("iface %p, adapter %u.\n", iface, adapter);
345
346     wined3d_mutex_lock();
347     ret = wined3d_get_adapter_monitor(d3d8->wined3d, adapter);
348     wined3d_mutex_unlock();
349
350     return ret;
351 }
352
353 static HRESULT WINAPI d3d8_CreateDevice(IDirect3D8 *iface, UINT adapter,
354         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
355         IDirect3DDevice8 **device)
356 {
357     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
358     struct d3d8_device *object;
359     HRESULT hr;
360
361     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
362             iface, adapter, device_type, focus_window, flags, parameters, device);
363
364     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
365     if (!object)
366     {
367         ERR("Failed to allocate device memory.\n");
368         return E_OUTOFMEMORY;
369     }
370
371     hr = device_init(object, d3d8, d3d8->wined3d, adapter, device_type, focus_window, flags, parameters);
372     if (FAILED(hr))
373     {
374         WARN("Failed to initialize device, hr %#x.\n", hr);
375         HeapFree(GetProcessHeap(), 0, object);
376         return hr;
377     }
378
379     TRACE("Created device %p.\n", object);
380     *device = &object->IDirect3DDevice8_iface;
381
382     return D3D_OK;
383 }
384
385 static const struct IDirect3D8Vtbl d3d8_vtbl =
386 {
387     /* IUnknown */
388     d3d8_QueryInterface,
389     d3d8_AddRef,
390     d3d8_Release,
391     /* IDirect3D8 */
392     d3d8_RegisterSoftwareDevice,
393     d3d8_GetAdapterCount,
394     d3d8_GetAdapterIdentifier,
395     d3d8_GetAdapterModeCount,
396     d3d8_EnumAdapterModes,
397     d3d8_GetAdapterDisplayMode,
398     d3d8_CheckDeviceType,
399     d3d8_CheckDeviceFormat,
400     d3d8_CheckDeviceMultiSampleType,
401     d3d8_CheckDepthStencilMatch,
402     d3d8_GetDeviceCaps,
403     d3d8_GetAdapterMonitor,
404     d3d8_CreateDevice,
405 };
406
407 BOOL d3d8_init(struct d3d8 *d3d8)
408 {
409     d3d8->IDirect3D8_iface.lpVtbl = &d3d8_vtbl;
410     d3d8->refcount = 1;
411
412     wined3d_mutex_lock();
413     d3d8->wined3d = wined3d_create(8, WINED3D_LEGACY_DEPTH_BIAS);
414     wined3d_mutex_unlock();
415     if (!d3d8->wined3d)
416         return FALSE;
417
418     return TRUE;
419 }