xref: /reactos/dll/directx/wine/d3d8/directx.c (revision 019f21ee)
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 #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"
33 
34 #include "d3d8_private.h"
35 
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 
38 static inline struct d3d8 *impl_from_IDirect3D8(IDirect3D8 *iface)
39 {
40     return CONTAINING_RECORD(iface, struct d3d8, IDirect3D8_iface);
41 }
42 
43 static HRESULT WINAPI d3d8_QueryInterface(IDirect3D8 *iface, REFIID riid, void **out)
44 {
45     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
46 
47     if (IsEqualGUID(riid, &IID_IDirect3D8)
48             || IsEqualGUID(riid, &IID_IUnknown))
49     {
50         IDirect3D8_AddRef(iface);
51         *out = iface;
52         return S_OK;
53     }
54 
55     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
56 
57     *out = NULL;
58     return E_NOINTERFACE;
59 }
60 
61 static ULONG WINAPI d3d8_AddRef(IDirect3D8 *iface)
62 {
63     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
64     ULONG refcount = InterlockedIncrement(&d3d8->refcount);
65 
66     TRACE("%p increasing refcount to %u.\n", iface, refcount);
67 
68     return refcount;
69 }
70 
71 static ULONG WINAPI d3d8_Release(IDirect3D8 *iface)
72 {
73     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
74     ULONG refcount = InterlockedDecrement(&d3d8->refcount);
75 
76     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
77 
78     if (!refcount)
79     {
80         wined3d_mutex_lock();
81         wined3d_decref(d3d8->wined3d);
82         wined3d_mutex_unlock();
83 
84         heap_free(d3d8);
85     }
86 
87     return refcount;
88 }
89 
90 static HRESULT WINAPI d3d8_RegisterSoftwareDevice(IDirect3D8 *iface, void *init_function)
91 {
92     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
93     HRESULT hr;
94 
95     TRACE("iface %p, init_function %p.\n", iface, init_function);
96 
97     wined3d_mutex_lock();
98     hr = wined3d_register_software_device(d3d8->wined3d, init_function);
99     wined3d_mutex_unlock();
100 
101     return hr;
102 }
103 
104 static UINT WINAPI d3d8_GetAdapterCount(IDirect3D8 *iface)
105 {
106     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
107     HRESULT hr;
108 
109     TRACE("iface %p.\n", iface);
110 
111     wined3d_mutex_lock();
112     hr = wined3d_get_adapter_count(d3d8->wined3d);
113     wined3d_mutex_unlock();
114 
115     return hr;
116 }
117 
118 static HRESULT WINAPI d3d8_GetAdapterIdentifier(IDirect3D8 *iface, UINT adapter,
119         DWORD flags, D3DADAPTER_IDENTIFIER8 *identifier)
120 {
121     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
122     struct wined3d_adapter_identifier adapter_id;
123     HRESULT hr;
124 
125     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
126             iface, adapter, flags, identifier);
127 
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 */
134 
135     wined3d_mutex_lock();
136     hr = wined3d_get_adapter_identifier(d3d8->wined3d, adapter, flags, &adapter_id);
137     wined3d_mutex_unlock();
138 
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;
146 
147     return hr;
148 }
149 
150 static UINT WINAPI d3d8_GetAdapterModeCount(IDirect3D8 *iface, UINT adapter)
151 {
152     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
153     HRESULT hr;
154 
155     TRACE("iface %p, adapter %u.\n", iface, adapter);
156 
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();
161 
162     return hr;
163 }
164 
165 static HRESULT WINAPI d3d8_EnumAdapterModes(IDirect3D8 *iface, UINT adapter, UINT mode_idx, D3DDISPLAYMODE *mode)
166 {
167     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
168     struct wined3d_display_mode wined3d_mode;
169     HRESULT hr;
170 
171     TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
172             iface, adapter, mode_idx, mode);
173 
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();
178 
179     if (SUCCEEDED(hr))
180     {
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);
185     }
186 
187     return hr;
188 }
189 
190 static HRESULT WINAPI d3d8_GetAdapterDisplayMode(IDirect3D8 *iface, UINT adapter, D3DDISPLAYMODE *mode)
191 {
192     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
193     struct wined3d_display_mode wined3d_mode;
194     HRESULT hr;
195 
196     TRACE("iface %p, adapter %u, mode %p.\n",
197             iface, adapter, mode);
198 
199     wined3d_mutex_lock();
200     hr = wined3d_get_adapter_display_mode(d3d8->wined3d, adapter, &wined3d_mode, NULL);
201     wined3d_mutex_unlock();
202 
203     if (SUCCEEDED(hr))
204     {
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);
209     }
210 
211     return hr;
212 }
213 
214 static HRESULT WINAPI d3d8_CheckDeviceType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
215         D3DFORMAT display_format, D3DFORMAT backbuffer_format, BOOL windowed)
216 {
217     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
218     HRESULT hr;
219 
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);
222 
223     if (!windowed && display_format != D3DFMT_X8R8G8B8 && display_format != D3DFMT_R5G6B5)
224         return WINED3DERR_NOTAVAILABLE;
225 
226     wined3d_mutex_lock();
227     hr = wined3d_check_device_type(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(display_format),
228             wined3dformat_from_d3dformat(backbuffer_format), windowed);
229     wined3d_mutex_unlock();
230 
231     return hr;
232 }
233 
234 static HRESULT WINAPI d3d8_CheckDeviceFormat(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
235         D3DFORMAT adapter_format, DWORD usage, D3DRESOURCETYPE resource_type, D3DFORMAT format)
236 {
237     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
238     enum wined3d_resource_type wined3d_rtype;
239     HRESULT hr;
240 
241     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
242             iface, adapter, device_type, adapter_format, usage, resource_type, format);
243 
244     if (!adapter_format)
245     {
246         WARN("Invalid adapter format.\n");
247         return D3DERR_INVALIDCALL;
248     }
249 
250     usage = usage & (WINED3DUSAGE_MASK | WINED3DUSAGE_QUERY_MASK);
251     switch (resource_type)
252     {
253         case D3DRTYPE_CUBETEXTURE:
254             usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
255         case D3DRTYPE_TEXTURE:
256             usage |= WINED3DUSAGE_TEXTURE;
257         case D3DRTYPE_SURFACE:
258             wined3d_rtype = WINED3D_RTYPE_TEXTURE_2D;
259             break;
260 
261         case D3DRTYPE_VOLUMETEXTURE:
262         case D3DRTYPE_VOLUME:
263             usage |= WINED3DUSAGE_TEXTURE;
264             wined3d_rtype = WINED3D_RTYPE_TEXTURE_3D;
265             break;
266 
267         case D3DRTYPE_VERTEXBUFFER:
268         case D3DRTYPE_INDEXBUFFER:
269             wined3d_rtype = WINED3D_RTYPE_BUFFER;
270             break;
271 
272         default:
273             FIXME("Unhandled resource type %#x.\n", resource_type);
274             return WINED3DERR_INVALIDCALL;
275     }
276 
277     wined3d_mutex_lock();
278     hr = wined3d_check_device_format(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(adapter_format),
279             usage, wined3d_rtype, wined3dformat_from_d3dformat(format));
280     wined3d_mutex_unlock();
281 
282     return hr;
283 }
284 
285 static HRESULT WINAPI d3d8_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
286         D3DFORMAT format, BOOL windowed, D3DMULTISAMPLE_TYPE multisample_type)
287 {
288     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
289     HRESULT hr;
290 
291     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
292             iface, adapter, device_type, format, windowed, multisample_type);
293 
294     if (multisample_type > D3DMULTISAMPLE_16_SAMPLES)
295         return D3DERR_INVALIDCALL;
296 
297     wined3d_mutex_lock();
298     hr = wined3d_check_device_multisample_type(d3d8->wined3d, adapter, device_type,
299             wined3dformat_from_d3dformat(format), windowed,
300             (enum wined3d_multisample_type)multisample_type, NULL);
301     wined3d_mutex_unlock();
302 
303     return hr;
304 }
305 
306 static HRESULT WINAPI d3d8_CheckDepthStencilMatch(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
307         D3DFORMAT adapter_format, D3DFORMAT rt_format, D3DFORMAT ds_format)
308 {
309     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
310     HRESULT hr;
311 
312     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
313             iface, adapter, device_type, adapter_format, rt_format, ds_format);
314 
315     wined3d_mutex_lock();
316     hr = wined3d_check_depth_stencil_match(d3d8->wined3d, adapter, device_type,
317             wined3dformat_from_d3dformat(adapter_format), wined3dformat_from_d3dformat(rt_format),
318             wined3dformat_from_d3dformat(ds_format));
319     wined3d_mutex_unlock();
320 
321     return hr;
322 }
323 
324 static HRESULT WINAPI d3d8_GetDeviceCaps(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type, D3DCAPS8 *caps)
325 {
326     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
327     WINED3DCAPS wined3d_caps;
328     HRESULT hr;
329 
330     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, adapter, device_type, caps);
331 
332     if (!caps)
333         return D3DERR_INVALIDCALL;
334 
335     wined3d_mutex_lock();
336     hr = wined3d_get_device_caps(d3d8->wined3d, adapter, device_type, &wined3d_caps);
337     wined3d_mutex_unlock();
338 
339     d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
340 
341     return hr;
342 }
343 
344 static HMONITOR WINAPI d3d8_GetAdapterMonitor(IDirect3D8 *iface, UINT adapter)
345 {
346     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
347     struct wined3d_output_desc desc;
348     HRESULT hr;
349 
350     TRACE("iface %p, adapter %u.\n", iface, adapter);
351 
352     wined3d_mutex_lock();
353     hr = wined3d_get_output_desc(d3d8->wined3d, adapter, &desc);
354     wined3d_mutex_unlock();
355 
356     if (FAILED(hr))
357     {
358         WARN("Failed to get output desc, hr %#x.\n", hr);
359         return NULL;
360     }
361 
362     return desc.monitor;
363 }
364 
365 static HRESULT WINAPI d3d8_CreateDevice(IDirect3D8 *iface, UINT adapter,
366         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
367         IDirect3DDevice8 **device)
368 {
369     struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
370     struct d3d8_device *object;
371     HRESULT hr;
372 
373     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
374             iface, adapter, device_type, focus_window, flags, parameters, device);
375 
376     if (!(object = heap_alloc_zero(sizeof(*object))))
377         return E_OUTOFMEMORY;
378 
379     hr = device_init(object, d3d8, d3d8->wined3d, adapter, device_type, focus_window, flags, parameters);
380     if (FAILED(hr))
381     {
382         WARN("Failed to initialize device, hr %#x.\n", hr);
383         heap_free(object);
384         return hr;
385     }
386 
387     TRACE("Created device %p.\n", object);
388     *device = &object->IDirect3DDevice8_iface;
389 
390     return D3D_OK;
391 }
392 
393 static const struct IDirect3D8Vtbl d3d8_vtbl =
394 {
395     /* IUnknown */
396     d3d8_QueryInterface,
397     d3d8_AddRef,
398     d3d8_Release,
399     /* IDirect3D8 */
400     d3d8_RegisterSoftwareDevice,
401     d3d8_GetAdapterCount,
402     d3d8_GetAdapterIdentifier,
403     d3d8_GetAdapterModeCount,
404     d3d8_EnumAdapterModes,
405     d3d8_GetAdapterDisplayMode,
406     d3d8_CheckDeviceType,
407     d3d8_CheckDeviceFormat,
408     d3d8_CheckDeviceMultiSampleType,
409     d3d8_CheckDepthStencilMatch,
410     d3d8_GetDeviceCaps,
411     d3d8_GetAdapterMonitor,
412     d3d8_CreateDevice,
413 };
414 
415 BOOL d3d8_init(struct d3d8 *d3d8)
416 {
417     DWORD flags = WINED3D_LEGACY_DEPTH_BIAS | WINED3D_VIDMEM_ACCOUNTING
418             | WINED3D_HANDLE_RESTORE | WINED3D_PIXEL_CENTER_INTEGER
419             | WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR | WINED3D_NO_PRIMITIVE_RESTART
420             | WINED3D_LEGACY_CUBEMAP_FILTERING | WINED3D_LIMIT_VIEWPORT;
421 
422     d3d8->IDirect3D8_iface.lpVtbl = &d3d8_vtbl;
423     d3d8->refcount = 1;
424 
425     wined3d_mutex_lock();
426     d3d8->wined3d = wined3d_create(flags);
427     wined3d_mutex_unlock();
428     if (!d3d8->wined3d)
429         return FALSE;
430 
431     return TRUE;
432 }
433