d3d: Handle the pointsize_min default difference in d3d8.
[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 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
42 {
43     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
44
45     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
46
47     if (IsEqualGUID(riid, &IID_IUnknown)
48         || IsEqualGUID(riid, &IID_IDirect3D8)) {
49         IUnknown_AddRef(iface);
50         *ppobj = This;
51         return S_OK;
52     }
53
54     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
55     *ppobj = NULL;
56     return E_NOINTERFACE;
57 }
58
59 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
60     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
61     ULONG ref = InterlockedIncrement(&This->ref);
62
63     TRACE("%p increasing refcount to %u.\n", iface, ref);
64
65     return ref;
66 }
67
68 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
69     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
70     ULONG ref = InterlockedDecrement(&This->ref);
71
72     TRACE("%p decreasing refcount to %u.\n", iface, ref);
73
74     if (ref == 0) {
75         TRACE("Releasing wined3d %p\n", This->WineD3D);
76
77         wined3d_mutex_lock();
78         IWineD3D_Release(This->WineD3D);
79         wined3d_mutex_unlock();
80
81         HeapFree(GetProcessHeap(), 0, This);
82     }
83
84     return ref;
85 }
86
87 /* IDirect3D8 Interface follow: */
88 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
89     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
90     HRESULT hr;
91
92     TRACE("iface %p, init_function %p.\n", iface, pInitializeFunction);
93
94     wined3d_mutex_lock();
95     hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
96     wined3d_mutex_unlock();
97
98     return hr;
99 }
100
101 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
102     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
103     HRESULT hr;
104
105     TRACE("iface %p.\n", iface);
106
107     wined3d_mutex_lock();
108     hr = IWineD3D_GetAdapterCount(This->WineD3D);
109     wined3d_mutex_unlock();
110
111     return hr;
112 }
113
114 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface,
115         UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
116 {
117     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
118     WINED3DADAPTER_IDENTIFIER adapter_id;
119     HRESULT hr;
120
121     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
122             iface, Adapter, Flags, pIdentifier);
123
124     adapter_id.driver = pIdentifier->Driver;
125     adapter_id.driver_size = sizeof(pIdentifier->Driver);
126     adapter_id.description = pIdentifier->Description;
127     adapter_id.description_size = sizeof(pIdentifier->Description);
128     adapter_id.device_name = NULL; /* d3d9 only */
129     adapter_id.device_name_size = 0; /* d3d9 only */
130
131     wined3d_mutex_lock();
132     hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
133     wined3d_mutex_unlock();
134
135     pIdentifier->DriverVersion = adapter_id.driver_version;
136     pIdentifier->VendorId = adapter_id.vendor_id;
137     pIdentifier->DeviceId = adapter_id.device_id;
138     pIdentifier->SubSysId = adapter_id.subsystem_id;
139     pIdentifier->Revision = adapter_id.revision;
140     memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
141     pIdentifier->WHQLLevel = adapter_id.whql_level;
142
143     return hr;
144 }
145
146 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
147     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
148     HRESULT hr;
149
150     TRACE("iface %p, adapter %u.\n", iface, Adapter);
151
152     wined3d_mutex_lock();
153     hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
154     wined3d_mutex_unlock();
155
156     return hr;
157 }
158
159 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
160     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
161     HRESULT hr;
162
163     TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
164             iface, Adapter, Mode, pMode);
165
166     wined3d_mutex_lock();
167     hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
168     wined3d_mutex_unlock();
169
170     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
171
172     return hr;
173 }
174
175 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
176     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
177     HRESULT hr;
178
179     TRACE("iface %p, adapter %u, mode %p.\n",
180             iface, Adapter, pMode);
181
182     wined3d_mutex_lock();
183     hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
184     wined3d_mutex_unlock();
185
186     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
187
188     return hr;
189 }
190
191 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
192                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
193                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
194     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
195     HRESULT hr;
196
197     TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
198             iface, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed);
199
200     wined3d_mutex_lock();
201     hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
202             wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
203     wined3d_mutex_unlock();
204
205     return hr;
206 }
207
208 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
209                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
210                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
211     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
212     HRESULT hr;
213     WINED3DRESOURCETYPE WineD3DRType;
214
215     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
216             iface, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
217
218     if(CheckFormat == D3DFMT_R8G8B8)
219     {
220         /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
221         WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
222         return D3DERR_NOTAVAILABLE;
223     }
224
225
226     switch(RType) {
227         case D3DRTYPE_VERTEXBUFFER:
228         case D3DRTYPE_INDEXBUFFER:
229             WineD3DRType = WINED3DRTYPE_BUFFER;
230             break;
231
232         default:
233             WineD3DRType = RType;
234             break;
235     }
236
237     wined3d_mutex_lock();
238     hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
239             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
240     wined3d_mutex_unlock();
241
242     return hr;
243 }
244
245 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT Adapter,
246         D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType)
247 {
248     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
249     HRESULT hr;
250
251     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
252             iface, Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType);
253
254     wined3d_mutex_lock();
255     hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
256             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
257     wined3d_mutex_unlock();
258
259     return hr;
260 }
261
262 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8 *iface, UINT Adapter, D3DDEVTYPE DeviceType,
263         D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
264 {
265     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
266     HRESULT hr;
267
268     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
269             iface, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
270
271     wined3d_mutex_lock();
272     hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
273             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
274             wined3dformat_from_d3dformat(DepthStencilFormat));
275     wined3d_mutex_unlock();
276
277     return hr;
278 }
279
280 void fixup_caps(WINED3DCAPS *caps)
281 {
282     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
283     if (caps->PixelShaderVersion > D3DPS_VERSION(1,4)) {
284         caps->PixelShaderVersion = D3DPS_VERSION(1,4);
285     }
286     if (caps->VertexShaderVersion > D3DVS_VERSION(1,1)) {
287         caps->VertexShaderVersion = D3DVS_VERSION(1,1);
288     }
289     caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
290
291     caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
292 }
293
294 static HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
295     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
296     HRESULT hrc = D3D_OK;
297     WINED3DCAPS *pWineCaps;
298
299     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, Adapter, DeviceType, pCaps);
300
301     if(NULL == pCaps){
302         return D3DERR_INVALIDCALL;
303     }
304     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
305     if(pWineCaps == NULL){
306         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
307     }
308
309     wined3d_mutex_lock();
310     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
311     wined3d_mutex_unlock();
312
313     fixup_caps(pWineCaps);
314     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
315     HeapFree(GetProcessHeap(), 0, pWineCaps);
316
317     TRACE("(%p) returning %p\n", This, pCaps);
318     return hrc;
319 }
320
321 static HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
322     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
323     HMONITOR ret;
324
325     TRACE("iface %p, adapter %u.\n", iface, Adapter);
326
327     wined3d_mutex_lock();
328     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
329     wined3d_mutex_unlock();
330
331     return ret;
332 }
333
334 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
335     IUnknown* swapChainParent;
336
337     TRACE("swapchain %p.\n", pSwapChain);
338
339     IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
340     IUnknown_Release(swapChainParent);
341     return IUnknown_Release(swapChainParent);
342 }
343
344 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
345                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
346                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
347
348     IDirect3D8Impl       *This   = (IDirect3D8Impl *)iface;
349     IDirect3DDevice8Impl *object = NULL;
350     WINED3DPRESENT_PARAMETERS localParameters;
351     HRESULT hr;
352
353     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
354             iface, Adapter, DeviceType, hFocusWindow, BehaviourFlags, pPresentationParameters,
355             ppReturnedDeviceInterface);
356
357     /* Check the validity range of the adapter parameter */
358     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
359         *ppReturnedDeviceInterface = NULL;
360         return D3DERR_INVALIDCALL;
361     }
362
363     /* Allocate the storage for the device object */
364     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
365     if (NULL == object) {
366         FIXME("Allocation of memory failed\n");
367         *ppReturnedDeviceInterface = NULL;
368         return D3DERR_OUTOFVIDEOMEMORY;
369     }
370
371     object->lpVtbl = &Direct3DDevice8_Vtbl;
372     object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
373     object->ref = 1;
374     object->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
375             D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*object->handle_table.entries));
376     object->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
377     *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
378
379     /* Allocate an associated WineD3DDevice object */
380     wined3d_mutex_lock();
381     hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
382             (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
383
384     if (hr != D3D_OK) {
385         HeapFree(GetProcessHeap(), 0, object);
386         *ppReturnedDeviceInterface = NULL;
387         wined3d_mutex_unlock();
388
389         return hr;
390     }
391
392     TRACE("(%p) : Created Device %p\n", This, object);
393
394     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
395     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
396     localParameters.BackBufferFormat                            = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
397     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
398     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
399     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
400     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
401     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
402     localParameters.Windowed                                    = pPresentationParameters->Windowed;
403     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
404     localParameters.AutoDepthStencilFormat                      = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
405     localParameters.Flags                                       = pPresentationParameters->Flags;
406     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
407     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
408     localParameters.AutoRestoreDisplayMode                      = TRUE;
409
410     if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
411         IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
412     }
413
414     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
415     if (hr != D3D_OK) {
416         wined3d_mutex_unlock();
417         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
418         goto err;
419     }
420     hr = IWineD3DDevice_SetRenderState(object->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
421     wined3d_mutex_unlock();
422     if(FAILED(hr)) {
423         FIXME("(%p) SetRenderState failed\n", This);
424         goto err;
425     }
426
427     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
428     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
429     pPresentationParameters->BackBufferFormat                   = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
430     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
431     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
432     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
433     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
434     pPresentationParameters->Windowed                           = localParameters.Windowed;
435     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
436     pPresentationParameters->AutoDepthStencilFormat             = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
437     pPresentationParameters->Flags                              = localParameters.Flags;
438     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
439     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
440
441     object->declArraySize = 16;
442     object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
443     if(!object->decls) {
444         ERR("Out of memory\n");
445         hr = E_OUTOFMEMORY;
446         goto err;
447     }
448     return D3D_OK;
449
450 err:
451     *ppReturnedDeviceInterface = NULL;
452
453     if(!object) return hr;
454     HeapFree(GetProcessHeap(), 0, object->decls);
455     if(object->WineD3DDevice) {
456         wined3d_mutex_lock();
457         IWineD3DDevice_Uninit3D(object->WineD3DDevice, D3D8CB_DestroySwapChain);
458         IWineD3DDevice_Release(object->WineD3DDevice);
459         wined3d_mutex_unlock();
460     }
461     HeapFree(GetProcessHeap(), 0, object);
462
463     return hr;
464 }
465
466 const IDirect3D8Vtbl Direct3D8_Vtbl =
467 {
468     /* IUnknown */
469     IDirect3D8Impl_QueryInterface,
470     IDirect3D8Impl_AddRef,
471     IDirect3D8Impl_Release,
472     /* IDirect3D8 */
473     IDirect3D8Impl_RegisterSoftwareDevice,
474     IDirect3D8Impl_GetAdapterCount,
475     IDirect3D8Impl_GetAdapterIdentifier,
476     IDirect3D8Impl_GetAdapterModeCount,
477     IDirect3D8Impl_EnumAdapterModes,
478     IDirect3D8Impl_GetAdapterDisplayMode,
479     IDirect3D8Impl_CheckDeviceType,
480     IDirect3D8Impl_CheckDeviceFormat,
481     IDirect3D8Impl_CheckDeviceMultiSampleType,
482     IDirect3D8Impl_CheckDepthStencilMatch,
483     IDirect3D8Impl_GetDeviceCaps,
484     IDirect3D8Impl_GetAdapterMonitor,
485     IDirect3D8Impl_CreateDevice
486 };