dxgi: Implement dxgi_output_GetDesc().
[wine/multimedia.git] / dlls / d3d8 / directx.c
blob12fdc022376e437bfdc6c697edaaba8bd0b0f8c4
1 /*
2 * IDirect3D8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
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.
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.
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
23 #include "config.h"
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "d3d8_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
38 static inline struct d3d8 *impl_from_IDirect3D8(IDirect3D8 *iface)
40 return CONTAINING_RECORD(iface, struct d3d8, IDirect3D8_iface);
43 static HRESULT WINAPI d3d8_QueryInterface(IDirect3D8 *iface, REFIID riid, void **out)
45 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
47 if (IsEqualGUID(riid, &IID_IDirect3D8)
48 || IsEqualGUID(riid, &IID_IUnknown))
50 IDirect3D8_AddRef(iface);
51 *out = iface;
52 return S_OK;
55 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
57 *out = NULL;
58 return E_NOINTERFACE;
61 static ULONG WINAPI d3d8_AddRef(IDirect3D8 *iface)
63 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
64 ULONG refcount = InterlockedIncrement(&d3d8->refcount);
66 TRACE("%p increasing refcount to %u.\n", iface, refcount);
68 return refcount;
71 static ULONG WINAPI d3d8_Release(IDirect3D8 *iface)
73 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
74 ULONG refcount = InterlockedDecrement(&d3d8->refcount);
76 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
78 if (!refcount)
80 wined3d_mutex_lock();
81 wined3d_decref(d3d8->wined3d);
82 wined3d_mutex_unlock();
84 HeapFree(GetProcessHeap(), 0, d3d8);
87 return refcount;
90 static HRESULT WINAPI d3d8_RegisterSoftwareDevice(IDirect3D8 *iface, void *init_function)
92 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
93 HRESULT hr;
95 TRACE("iface %p, init_function %p.\n", iface, init_function);
97 wined3d_mutex_lock();
98 hr = wined3d_register_software_device(d3d8->wined3d, init_function);
99 wined3d_mutex_unlock();
101 return hr;
104 static UINT WINAPI d3d8_GetAdapterCount(IDirect3D8 *iface)
106 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
107 HRESULT hr;
109 TRACE("iface %p.\n", iface);
111 wined3d_mutex_lock();
112 hr = wined3d_get_adapter_count(d3d8->wined3d);
113 wined3d_mutex_unlock();
115 return hr;
118 static HRESULT WINAPI d3d8_GetAdapterIdentifier(IDirect3D8 *iface, UINT adapter,
119 DWORD flags, D3DADAPTER_IDENTIFIER8 *identifier)
121 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
122 struct wined3d_adapter_identifier adapter_id;
123 HRESULT hr;
125 TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
126 iface, adapter, flags, identifier);
128 adapter_id.driver = identifier->Driver;
129 adapter_id.driver_size = sizeof(identifier->Driver);
130 adapter_id.description = identifier->Description;
131 adapter_id.description_size = sizeof(identifier->Description);
132 adapter_id.device_name = NULL; /* d3d9 only */
133 adapter_id.device_name_size = 0; /* d3d9 only */
135 wined3d_mutex_lock();
136 hr = wined3d_get_adapter_identifier(d3d8->wined3d, adapter, flags, &adapter_id);
137 wined3d_mutex_unlock();
139 identifier->DriverVersion = adapter_id.driver_version;
140 identifier->VendorId = adapter_id.vendor_id;
141 identifier->DeviceId = adapter_id.device_id;
142 identifier->SubSysId = adapter_id.subsystem_id;
143 identifier->Revision = adapter_id.revision;
144 memcpy(&identifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(identifier->DeviceIdentifier));
145 identifier->WHQLLevel = adapter_id.whql_level;
147 return hr;
150 static UINT WINAPI d3d8_GetAdapterModeCount(IDirect3D8 *iface, UINT adapter)
152 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
153 HRESULT hr;
155 TRACE("iface %p, adapter %u.\n", iface, adapter);
157 wined3d_mutex_lock();
158 hr = wined3d_get_adapter_mode_count(d3d8->wined3d, adapter,
159 WINED3DFMT_UNKNOWN, WINED3D_SCANLINE_ORDERING_UNKNOWN);
160 wined3d_mutex_unlock();
162 return hr;
165 static HRESULT WINAPI d3d8_EnumAdapterModes(IDirect3D8 *iface, UINT adapter, UINT mode_idx, D3DDISPLAYMODE *mode)
167 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
168 struct wined3d_display_mode wined3d_mode;
169 HRESULT hr;
171 TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
172 iface, adapter, mode_idx, mode);
174 wined3d_mutex_lock();
175 hr = wined3d_enum_adapter_modes(d3d8->wined3d, adapter, WINED3DFMT_UNKNOWN,
176 WINED3D_SCANLINE_ORDERING_UNKNOWN, mode_idx, &wined3d_mode);
177 wined3d_mutex_unlock();
179 if (SUCCEEDED(hr))
181 mode->Width = wined3d_mode.width;
182 mode->Height = wined3d_mode.height;
183 mode->RefreshRate = wined3d_mode.refresh_rate;
184 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
187 return hr;
190 static HRESULT WINAPI d3d8_GetAdapterDisplayMode(IDirect3D8 *iface, UINT adapter, D3DDISPLAYMODE *mode)
192 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
193 struct wined3d_display_mode wined3d_mode;
194 HRESULT hr;
196 TRACE("iface %p, adapter %u, mode %p.\n",
197 iface, adapter, mode);
199 wined3d_mutex_lock();
200 hr = wined3d_get_adapter_display_mode(d3d8->wined3d, adapter, &wined3d_mode, NULL);
201 wined3d_mutex_unlock();
203 if (SUCCEEDED(hr))
205 mode->Width = wined3d_mode.width;
206 mode->Height = wined3d_mode.height;
207 mode->RefreshRate = wined3d_mode.refresh_rate;
208 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
211 return hr;
214 static HRESULT WINAPI d3d8_CheckDeviceType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
215 D3DFORMAT display_format, D3DFORMAT backbuffer_format, BOOL windowed)
217 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
218 HRESULT hr;
220 TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
221 iface, adapter, device_type, display_format, backbuffer_format, windowed);
223 wined3d_mutex_lock();
224 hr = wined3d_check_device_type(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(display_format),
225 wined3dformat_from_d3dformat(backbuffer_format), windowed);
226 wined3d_mutex_unlock();
228 return hr;
231 static HRESULT WINAPI d3d8_CheckDeviceFormat(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
232 D3DFORMAT adapter_format, DWORD usage, D3DRESOURCETYPE resource_type, D3DFORMAT format)
234 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
235 enum wined3d_resource_type wined3d_rtype;
236 HRESULT hr;
238 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
239 iface, adapter, device_type, adapter_format, usage, resource_type, format);
241 switch (resource_type)
243 case D3DRTYPE_VERTEXBUFFER:
244 case D3DRTYPE_INDEXBUFFER:
245 wined3d_rtype = WINED3D_RTYPE_BUFFER;
246 break;
248 default:
249 wined3d_rtype = resource_type;
250 break;
253 wined3d_mutex_lock();
254 hr = wined3d_check_device_format(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(adapter_format),
255 usage, wined3d_rtype, wined3dformat_from_d3dformat(format));
256 wined3d_mutex_unlock();
258 return hr;
261 static HRESULT WINAPI d3d8_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
262 D3DFORMAT format, BOOL windowed, D3DMULTISAMPLE_TYPE multisample_type)
264 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
265 HRESULT hr;
267 TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
268 iface, adapter, device_type, format, windowed, multisample_type);
270 wined3d_mutex_lock();
271 hr = wined3d_check_device_multisample_type(d3d8->wined3d, adapter, device_type,
272 wined3dformat_from_d3dformat(format), windowed,
273 (enum wined3d_multisample_type)multisample_type, NULL);
274 wined3d_mutex_unlock();
276 return hr;
279 static HRESULT WINAPI d3d8_CheckDepthStencilMatch(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
280 D3DFORMAT adapter_format, D3DFORMAT rt_format, D3DFORMAT ds_format)
282 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
283 HRESULT hr;
285 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
286 iface, adapter, device_type, adapter_format, rt_format, ds_format);
288 wined3d_mutex_lock();
289 hr = wined3d_check_depth_stencil_match(d3d8->wined3d, adapter, device_type,
290 wined3dformat_from_d3dformat(adapter_format), wined3dformat_from_d3dformat(rt_format),
291 wined3dformat_from_d3dformat(ds_format));
292 wined3d_mutex_unlock();
294 return hr;
297 void fixup_caps(WINED3DCAPS *caps)
299 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
300 if (caps->PixelShaderVersion)
301 caps->PixelShaderVersion = D3DPS_VERSION(1,4);
302 else
303 caps->PixelShaderVersion = D3DPS_VERSION(0,0);
304 if (caps->VertexShaderVersion)
305 caps->VertexShaderVersion = D3DVS_VERSION(1,1);
306 else
307 caps->VertexShaderVersion = D3DVS_VERSION(0,0);
308 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
310 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
313 static HRESULT WINAPI d3d8_GetDeviceCaps(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type, D3DCAPS8 *caps)
315 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
316 WINED3DCAPS *wined3d_caps;
317 HRESULT hr;
319 TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, adapter, device_type, caps);
321 if (!caps)
322 return D3DERR_INVALIDCALL;
324 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
325 return D3DERR_INVALIDCALL;
327 wined3d_mutex_lock();
328 hr = wined3d_get_device_caps(d3d8->wined3d, adapter, device_type, wined3d_caps);
329 wined3d_mutex_unlock();
331 fixup_caps(wined3d_caps);
332 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
333 HeapFree(GetProcessHeap(), 0, wined3d_caps);
335 return hr;
338 static HMONITOR WINAPI d3d8_GetAdapterMonitor(IDirect3D8 *iface, UINT adapter)
340 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
341 struct wined3d_output_desc desc;
342 HRESULT hr;
344 TRACE("iface %p, adapter %u.\n", iface, adapter);
346 wined3d_mutex_lock();
347 hr = wined3d_get_output_desc(d3d8->wined3d, adapter, &desc);
348 wined3d_mutex_unlock();
350 if (FAILED(hr))
352 WARN("Failed to get output desc, hr %#x.\n", hr);
353 return NULL;
356 return desc.monitor;
359 static HRESULT WINAPI d3d8_CreateDevice(IDirect3D8 *iface, UINT adapter,
360 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
361 IDirect3DDevice8 **device)
363 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
364 struct d3d8_device *object;
365 HRESULT hr;
367 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
368 iface, adapter, device_type, focus_window, flags, parameters, device);
370 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
371 if (!object)
372 return E_OUTOFMEMORY;
374 hr = device_init(object, d3d8, d3d8->wined3d, adapter, device_type, focus_window, flags, parameters);
375 if (FAILED(hr))
377 WARN("Failed to initialize device, hr %#x.\n", hr);
378 HeapFree(GetProcessHeap(), 0, object);
379 return hr;
382 TRACE("Created device %p.\n", object);
383 *device = &object->IDirect3DDevice8_iface;
385 return D3D_OK;
388 static const struct IDirect3D8Vtbl d3d8_vtbl =
390 /* IUnknown */
391 d3d8_QueryInterface,
392 d3d8_AddRef,
393 d3d8_Release,
394 /* IDirect3D8 */
395 d3d8_RegisterSoftwareDevice,
396 d3d8_GetAdapterCount,
397 d3d8_GetAdapterIdentifier,
398 d3d8_GetAdapterModeCount,
399 d3d8_EnumAdapterModes,
400 d3d8_GetAdapterDisplayMode,
401 d3d8_CheckDeviceType,
402 d3d8_CheckDeviceFormat,
403 d3d8_CheckDeviceMultiSampleType,
404 d3d8_CheckDepthStencilMatch,
405 d3d8_GetDeviceCaps,
406 d3d8_GetAdapterMonitor,
407 d3d8_CreateDevice,
410 BOOL d3d8_init(struct d3d8 *d3d8)
412 DWORD flags = WINED3D_LEGACY_DEPTH_BIAS | WINED3D_VIDMEM_ACCOUNTING
413 | WINED3D_HANDLE_RESTORE | WINED3D_PIXEL_CENTER_INTEGER;
415 d3d8->IDirect3D8_iface.lpVtbl = &d3d8_vtbl;
416 d3d8->refcount = 1;
418 wined3d_mutex_lock();
419 d3d8->wined3d = wined3d_create(flags);
420 wined3d_mutex_unlock();
421 if (!d3d8->wined3d)
422 return FALSE;
424 return TRUE;