windowscodecs/tests: Fix compilation on systems that don't support nameless structs.
[wine/multimedia.git] / dlls / d3d8 / directx.c
blob224a063d4582171002cf29ad3da1b6b6b611aaf3
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 #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"
36 #include "d3d8_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
40 static inline struct d3d8 *impl_from_IDirect3D8(IDirect3D8 *iface)
42 return CONTAINING_RECORD(iface, struct d3d8, IDirect3D8_iface);
45 static HRESULT WINAPI d3d8_QueryInterface(IDirect3D8 *iface, REFIID riid, void **out)
47 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
49 if (IsEqualGUID(riid, &IID_IDirect3D8)
50 || IsEqualGUID(riid, &IID_IUnknown))
52 IUnknown_AddRef(iface);
53 *out = iface;
54 return S_OK;
57 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
59 *out = NULL;
60 return E_NOINTERFACE;
63 static ULONG WINAPI d3d8_AddRef(IDirect3D8 *iface)
65 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
66 ULONG refcount = InterlockedIncrement(&d3d8->refcount);
68 TRACE("%p increasing refcount to %u.\n", iface, refcount);
70 return refcount;
73 static ULONG WINAPI d3d8_Release(IDirect3D8 *iface)
75 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
76 ULONG refcount = InterlockedDecrement(&d3d8->refcount);
78 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
80 if (!refcount)
82 wined3d_mutex_lock();
83 wined3d_decref(d3d8->wined3d);
84 wined3d_mutex_unlock();
86 HeapFree(GetProcessHeap(), 0, d3d8);
89 return refcount;
92 static HRESULT WINAPI d3d8_RegisterSoftwareDevice(IDirect3D8 *iface, void *init_function)
94 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
95 HRESULT hr;
97 TRACE("iface %p, init_function %p.\n", iface, init_function);
99 wined3d_mutex_lock();
100 hr = wined3d_register_software_device(d3d8->wined3d, init_function);
101 wined3d_mutex_unlock();
103 return hr;
106 static UINT WINAPI d3d8_GetAdapterCount(IDirect3D8 *iface)
108 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
109 HRESULT hr;
111 TRACE("iface %p.\n", iface);
113 wined3d_mutex_lock();
114 hr = wined3d_get_adapter_count(d3d8->wined3d);
115 wined3d_mutex_unlock();
117 return hr;
120 static HRESULT WINAPI d3d8_GetAdapterIdentifier(IDirect3D8 *iface, UINT adapter,
121 DWORD flags, D3DADAPTER_IDENTIFIER8 *identifier)
123 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
124 struct wined3d_adapter_identifier adapter_id;
125 HRESULT hr;
127 TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
128 iface, adapter, flags, identifier);
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 */
137 wined3d_mutex_lock();
138 hr = wined3d_get_adapter_identifier(d3d8->wined3d, adapter, flags, &adapter_id);
139 wined3d_mutex_unlock();
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;
149 return hr;
152 static UINT WINAPI d3d8_GetAdapterModeCount(IDirect3D8 *iface, UINT adapter)
154 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
155 HRESULT hr;
157 TRACE("iface %p, adapter %u.\n", iface, adapter);
159 wined3d_mutex_lock();
160 hr = wined3d_get_adapter_mode_count(d3d8->wined3d, adapter, 0);
161 wined3d_mutex_unlock();
163 return hr;
166 static HRESULT WINAPI d3d8_EnumAdapterModes(IDirect3D8 *iface, UINT adapter, UINT mode_idx, D3DDISPLAYMODE *mode)
168 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
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 mode_idx, (struct wined3d_display_mode *)mode);
177 wined3d_mutex_unlock();
179 if (SUCCEEDED(hr))
180 mode->Format = d3dformat_from_wined3dformat(mode->Format);
182 return hr;
185 static HRESULT WINAPI d3d8_GetAdapterDisplayMode(IDirect3D8 *iface, UINT adapter, D3DDISPLAYMODE *mode)
187 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
188 HRESULT hr;
190 TRACE("iface %p, adapter %u, mode %p.\n",
191 iface, adapter, mode);
193 wined3d_mutex_lock();
194 hr = wined3d_get_adapter_display_mode(d3d8->wined3d, adapter, (struct wined3d_display_mode *)mode);
195 wined3d_mutex_unlock();
197 if (SUCCEEDED(hr))
198 mode->Format = d3dformat_from_wined3dformat(mode->Format);
200 return hr;
203 static HRESULT WINAPI d3d8_CheckDeviceType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
204 D3DFORMAT display_format, D3DFORMAT backbuffer_format, BOOL windowed)
206 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
207 HRESULT hr;
209 TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
210 iface, adapter, device_type, display_format, backbuffer_format, windowed);
212 wined3d_mutex_lock();
213 hr = wined3d_check_device_type(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(display_format),
214 wined3dformat_from_d3dformat(backbuffer_format), windowed);
215 wined3d_mutex_unlock();
217 return hr;
220 static HRESULT WINAPI d3d8_CheckDeviceFormat(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
221 D3DFORMAT adapter_format, DWORD usage, D3DRESOURCETYPE resource_type, D3DFORMAT format)
223 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
224 enum wined3d_resource_type wined3d_rtype;
225 HRESULT hr;
227 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
228 iface, adapter, device_type, adapter_format, usage, resource_type, format);
230 switch (resource_type)
232 case D3DRTYPE_VERTEXBUFFER:
233 case D3DRTYPE_INDEXBUFFER:
234 wined3d_rtype = WINED3D_RTYPE_BUFFER;
235 break;
237 default:
238 wined3d_rtype = resource_type;
239 break;
242 wined3d_mutex_lock();
243 hr = wined3d_check_device_format(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(adapter_format),
244 usage, wined3d_rtype, wined3dformat_from_d3dformat(format), WINED3D_SURFACE_TYPE_OPENGL);
245 wined3d_mutex_unlock();
247 return hr;
250 static HRESULT WINAPI d3d8_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
251 D3DFORMAT format, BOOL windowed, D3DMULTISAMPLE_TYPE multisample_type)
253 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
254 HRESULT hr;
256 TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
257 iface, adapter, device_type, format, windowed, multisample_type);
259 wined3d_mutex_lock();
260 hr = wined3d_check_device_multisample_type(d3d8->wined3d, adapter, device_type,
261 wined3dformat_from_d3dformat(format), windowed,
262 (enum wined3d_multisample_type)multisample_type, NULL);
263 wined3d_mutex_unlock();
265 return hr;
268 static HRESULT WINAPI d3d8_CheckDepthStencilMatch(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
269 D3DFORMAT adapter_format, D3DFORMAT rt_format, D3DFORMAT ds_format)
271 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
272 HRESULT hr;
274 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
275 iface, adapter, device_type, adapter_format, rt_format, ds_format);
277 wined3d_mutex_lock();
278 hr = wined3d_check_depth_stencil_match(d3d8->wined3d, adapter, device_type,
279 wined3dformat_from_d3dformat(adapter_format), wined3dformat_from_d3dformat(rt_format),
280 wined3dformat_from_d3dformat(ds_format));
281 wined3d_mutex_unlock();
283 return hr;
286 void fixup_caps(WINED3DCAPS *caps)
288 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
289 if (caps->PixelShaderVersion)
290 caps->PixelShaderVersion = D3DPS_VERSION(1,4);
291 else
292 caps->PixelShaderVersion = D3DPS_VERSION(0,0);
293 if (caps->VertexShaderVersion)
294 caps->VertexShaderVersion = D3DVS_VERSION(1,1);
295 else
296 caps->VertexShaderVersion = D3DVS_VERSION(0,0);
297 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
299 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
302 static HRESULT WINAPI d3d8_GetDeviceCaps(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type, D3DCAPS8 *caps)
304 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
305 WINED3DCAPS *wined3d_caps;
306 HRESULT hr;
308 TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, adapter, device_type, caps);
310 if (!caps)
311 return D3DERR_INVALIDCALL;
313 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
314 return D3DERR_INVALIDCALL;
316 wined3d_mutex_lock();
317 hr = wined3d_get_device_caps(d3d8->wined3d, adapter, device_type, wined3d_caps);
318 wined3d_mutex_unlock();
320 fixup_caps(wined3d_caps);
321 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
322 HeapFree(GetProcessHeap(), 0, wined3d_caps);
324 return hr;
327 static HMONITOR WINAPI d3d8_GetAdapterMonitor(IDirect3D8 *iface, UINT adapter)
329 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
330 HMONITOR ret;
332 TRACE("iface %p, adapter %u.\n", iface, adapter);
334 wined3d_mutex_lock();
335 ret = wined3d_get_adapter_monitor(d3d8->wined3d, adapter);
336 wined3d_mutex_unlock();
338 return ret;
341 static HRESULT WINAPI d3d8_CreateDevice(IDirect3D8 *iface, UINT adapter,
342 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
343 IDirect3DDevice8 **device)
345 struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
346 struct d3d8_device *object;
347 HRESULT hr;
349 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
350 iface, adapter, device_type, focus_window, flags, parameters, device);
352 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
353 if (!object)
355 ERR("Failed to allocate device memory.\n");
356 return E_OUTOFMEMORY;
359 hr = device_init(object, d3d8, d3d8->wined3d, adapter, device_type, focus_window, flags, parameters);
360 if (FAILED(hr))
362 WARN("Failed to initialize device, hr %#x.\n", hr);
363 HeapFree(GetProcessHeap(), 0, object);
364 return hr;
367 TRACE("Created device %p.\n", object);
368 *device = &object->IDirect3DDevice8_iface;
370 return D3D_OK;
373 static const struct IDirect3D8Vtbl d3d8_vtbl =
375 /* IUnknown */
376 d3d8_QueryInterface,
377 d3d8_AddRef,
378 d3d8_Release,
379 /* IDirect3D8 */
380 d3d8_RegisterSoftwareDevice,
381 d3d8_GetAdapterCount,
382 d3d8_GetAdapterIdentifier,
383 d3d8_GetAdapterModeCount,
384 d3d8_EnumAdapterModes,
385 d3d8_GetAdapterDisplayMode,
386 d3d8_CheckDeviceType,
387 d3d8_CheckDeviceFormat,
388 d3d8_CheckDeviceMultiSampleType,
389 d3d8_CheckDepthStencilMatch,
390 d3d8_GetDeviceCaps,
391 d3d8_GetAdapterMonitor,
392 d3d8_CreateDevice,
395 BOOL d3d8_init(struct d3d8 *d3d8)
397 d3d8->IDirect3D8_iface.lpVtbl = &d3d8_vtbl;
398 d3d8->refcount = 1;
400 wined3d_mutex_lock();
401 d3d8->wined3d = wined3d_create(8, WINED3D_LEGACY_DEPTH_BIAS);
402 wined3d_mutex_unlock();
403 if (!d3d8->wined3d)
404 return FALSE;
406 return TRUE;