ws2_32: Define sipx_network / sipx_node for DragonFly BSD.
[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 IDirect3D8Impl *impl_from_IDirect3D8(IDirect3D8 *iface)
41 {
42     return CONTAINING_RECORD(iface, IDirect3D8Impl, IDirect3D8_iface);
43 }
44
45 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
46 {
47     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
48
49     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
50
51     if (IsEqualGUID(riid, &IID_IUnknown)
52         || IsEqualGUID(riid, &IID_IDirect3D8)) {
53         IUnknown_AddRef(iface);
54         *ppobj = This;
55         return S_OK;
56     }
57
58     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
59     *ppobj = NULL;
60     return E_NOINTERFACE;
61 }
62
63 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface)
64 {
65     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
66     ULONG ref = InterlockedIncrement(&This->ref);
67
68     TRACE("%p increasing refcount to %u.\n", iface, ref);
69
70     return ref;
71 }
72
73 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface)
74 {
75     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
76     ULONG ref = InterlockedDecrement(&This->ref);
77
78     TRACE("%p decreasing refcount to %u.\n", iface, ref);
79
80     if (ref == 0) {
81         TRACE("Releasing wined3d %p\n", This->WineD3D);
82
83         wined3d_mutex_lock();
84         wined3d_decref(This->WineD3D);
85         wined3d_mutex_unlock();
86
87         HeapFree(GetProcessHeap(), 0, This);
88     }
89
90     return ref;
91 }
92
93 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice(LPDIRECT3D8 iface,
94         void* pInitializeFunction)
95 {
96     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
97     HRESULT hr;
98
99     TRACE("iface %p, init_function %p.\n", iface, pInitializeFunction);
100
101     wined3d_mutex_lock();
102     hr = wined3d_register_software_device(This->WineD3D, pInitializeFunction);
103     wined3d_mutex_unlock();
104
105     return hr;
106 }
107
108 static UINT WINAPI IDirect3D8Impl_GetAdapterCount(LPDIRECT3D8 iface)
109 {
110     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
111     HRESULT hr;
112
113     TRACE("iface %p.\n", iface);
114
115     wined3d_mutex_lock();
116     hr = wined3d_get_adapter_count(This->WineD3D);
117     wined3d_mutex_unlock();
118
119     return hr;
120 }
121
122 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface, UINT Adapter,
123         DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
124 {
125     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
126     WINED3DADAPTER_IDENTIFIER adapter_id;
127     HRESULT hr;
128
129     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
130             iface, Adapter, Flags, pIdentifier);
131
132     adapter_id.driver = pIdentifier->Driver;
133     adapter_id.driver_size = sizeof(pIdentifier->Driver);
134     adapter_id.description = pIdentifier->Description;
135     adapter_id.description_size = sizeof(pIdentifier->Description);
136     adapter_id.device_name = NULL; /* d3d9 only */
137     adapter_id.device_name_size = 0; /* d3d9 only */
138
139     wined3d_mutex_lock();
140     hr = wined3d_get_adapter_identifier(This->WineD3D, Adapter, Flags, &adapter_id);
141     wined3d_mutex_unlock();
142
143     pIdentifier->DriverVersion = adapter_id.driver_version;
144     pIdentifier->VendorId = adapter_id.vendor_id;
145     pIdentifier->DeviceId = adapter_id.device_id;
146     pIdentifier->SubSysId = adapter_id.subsystem_id;
147     pIdentifier->Revision = adapter_id.revision;
148     memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
149     pIdentifier->WHQLLevel = adapter_id.whql_level;
150
151     return hr;
152 }
153
154 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount(LPDIRECT3D8 iface,UINT Adapter)
155 {
156     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
157     HRESULT hr;
158
159     TRACE("iface %p, adapter %u.\n", iface, Adapter);
160
161     wined3d_mutex_lock();
162     hr = wined3d_get_adapter_mode_count(This->WineD3D, Adapter, 0);
163     wined3d_mutex_unlock();
164
165     return hr;
166 }
167
168 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes(LPDIRECT3D8 iface, UINT Adapter, UINT Mode,
169         D3DDISPLAYMODE* pMode)
170 {
171     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
172     HRESULT hr;
173
174     TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
175             iface, Adapter, Mode, pMode);
176
177     wined3d_mutex_lock();
178     hr = wined3d_enum_adapter_modes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *)pMode);
179     wined3d_mutex_unlock();
180
181     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
182
183     return hr;
184 }
185
186 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode(LPDIRECT3D8 iface, UINT Adapter,
187         D3DDISPLAYMODE* pMode)
188 {
189     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
190     HRESULT hr;
191
192     TRACE("iface %p, adapter %u, mode %p.\n",
193             iface, Adapter, pMode);
194
195     wined3d_mutex_lock();
196     hr = wined3d_get_adapter_display_mode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *)pMode);
197     wined3d_mutex_unlock();
198
199     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
200
201     return hr;
202 }
203
204 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceType(LPDIRECT3D8 iface, UINT Adapter,
205         D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat, D3DFORMAT BackBufferFormat, BOOL Windowed)
206 {
207     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
208     HRESULT hr;
209
210     TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
211             iface, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed);
212
213     wined3d_mutex_lock();
214     hr = wined3d_check_device_type(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
215             wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
216     wined3d_mutex_unlock();
217
218     return hr;
219 }
220
221 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat(LPDIRECT3D8 iface, UINT Adapter,
222         D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType,
223         D3DFORMAT CheckFormat)
224 {
225     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
226     HRESULT hr;
227     WINED3DRESOURCETYPE WineD3DRType;
228
229     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
230             iface, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
231
232     if(CheckFormat == D3DFMT_R8G8B8)
233     {
234         /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
235         WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
236         return D3DERR_NOTAVAILABLE;
237     }
238
239
240     switch(RType) {
241         case D3DRTYPE_VERTEXBUFFER:
242         case D3DRTYPE_INDEXBUFFER:
243             WineD3DRType = WINED3DRTYPE_BUFFER;
244             break;
245
246         default:
247             WineD3DRType = RType;
248             break;
249     }
250
251     wined3d_mutex_lock();
252     hr = wined3d_check_device_format(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
253             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
254     wined3d_mutex_unlock();
255
256     return hr;
257 }
258
259 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT Adapter,
260         D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed,
261         D3DMULTISAMPLE_TYPE MultiSampleType)
262 {
263     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
264     HRESULT hr;
265
266     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
267             iface, Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType);
268
269     wined3d_mutex_lock();
270     hr = wined3d_check_device_multisample_type(This->WineD3D, Adapter, DeviceType,
271             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE)MultiSampleType, NULL);
272     wined3d_mutex_unlock();
273
274     return hr;
275 }
276
277 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8 *iface, UINT Adapter,
278         D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat,
279         D3DFORMAT DepthStencilFormat)
280 {
281     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
282     HRESULT hr;
283
284     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
285             iface, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
286
287     wined3d_mutex_lock();
288     hr = wined3d_check_depth_stencil_match(This->WineD3D, Adapter, DeviceType,
289             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
290             wined3dformat_from_d3dformat(DepthStencilFormat));
291     wined3d_mutex_unlock();
292
293     return hr;
294 }
295
296 void fixup_caps(WINED3DCAPS *caps)
297 {
298     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
299     if (caps->PixelShaderVersion > D3DPS_VERSION(1,4)) {
300         caps->PixelShaderVersion = D3DPS_VERSION(1,4);
301     }
302     if (caps->VertexShaderVersion > D3DVS_VERSION(1,1)) {
303         caps->VertexShaderVersion = D3DVS_VERSION(1,1);
304     }
305     caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
306
307     caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
308 }
309
310 static HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter,
311         D3DDEVTYPE DeviceType, D3DCAPS8* pCaps)
312 {
313     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
314     HRESULT hrc = D3D_OK;
315     WINED3DCAPS *pWineCaps;
316
317     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, Adapter, DeviceType, pCaps);
318
319     if(NULL == pCaps){
320         return D3DERR_INVALIDCALL;
321     }
322     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
323     if(pWineCaps == NULL){
324         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
325     }
326
327     wined3d_mutex_lock();
328     hrc = wined3d_get_device_caps(This->WineD3D, Adapter, DeviceType, pWineCaps);
329     wined3d_mutex_unlock();
330
331     fixup_caps(pWineCaps);
332     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
333     HeapFree(GetProcessHeap(), 0, pWineCaps);
334
335     TRACE("(%p) returning %p\n", This, pCaps);
336     return hrc;
337 }
338
339 static HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter)
340 {
341     IDirect3D8Impl *This = 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(This->WineD3D, Adapter);
348     wined3d_mutex_unlock();
349
350     return ret;
351 }
352
353 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(IDirect3D8 *iface, UINT adapter,
354         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
355         IDirect3DDevice8 **device)
356 {
357     IDirect3D8Impl *This = impl_from_IDirect3D8(iface);
358     IDirect3DDevice8Impl *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, This, This->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 const IDirect3D8Vtbl Direct3D8_Vtbl =
386 {
387     /* IUnknown */
388     IDirect3D8Impl_QueryInterface,
389     IDirect3D8Impl_AddRef,
390     IDirect3D8Impl_Release,
391     /* IDirect3D8 */
392     IDirect3D8Impl_RegisterSoftwareDevice,
393     IDirect3D8Impl_GetAdapterCount,
394     IDirect3D8Impl_GetAdapterIdentifier,
395     IDirect3D8Impl_GetAdapterModeCount,
396     IDirect3D8Impl_EnumAdapterModes,
397     IDirect3D8Impl_GetAdapterDisplayMode,
398     IDirect3D8Impl_CheckDeviceType,
399     IDirect3D8Impl_CheckDeviceFormat,
400     IDirect3D8Impl_CheckDeviceMultiSampleType,
401     IDirect3D8Impl_CheckDepthStencilMatch,
402     IDirect3D8Impl_GetDeviceCaps,
403     IDirect3D8Impl_GetAdapterMonitor,
404     IDirect3D8Impl_CreateDevice
405 };