- better traces
[wine] / dlls / d3d9 / directx.c
1 /*
2  * IDirect3D9 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "d3d9_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25
26 /* IDirect3D9 IUnknown parts follow: */
27 HRESULT WINAPI IDirect3D9Impl_QueryInterface(LPDIRECT3D9 iface, REFIID riid, LPVOID* ppobj)
28 {
29     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3D9)) {
33         IDirect3D9Impl_AddRef(iface);
34         *ppobj = This;
35         return D3D_OK;
36     }
37
38     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39     return E_NOINTERFACE;
40 }
41
42 ULONG WINAPI IDirect3D9Impl_AddRef(LPDIRECT3D9 iface) {
43     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
44     ULONG ref = InterlockedIncrement(&This->ref);
45
46     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
47
48     return ref;
49 }
50
51 ULONG WINAPI IDirect3D9Impl_Release(LPDIRECT3D9 iface) {
52     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
53     ULONG ref = InterlockedDecrement(&This->ref);
54
55     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
56
57     if (ref == 0) {
58         IWineD3D_Release(This->WineD3D);
59         HeapFree(GetProcessHeap(), 0, This);
60     }
61
62     return ref;
63 }
64
65 /* IDirect3D9 Interface follow: */
66 HRESULT  WINAPI  IDirect3D9Impl_RegisterSoftwareDevice(LPDIRECT3D9 iface, void* pInitializeFunction) {
67     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
68     return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
69 }
70
71 UINT     WINAPI  IDirect3D9Impl_GetAdapterCount(LPDIRECT3D9 iface) {
72     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
73     return IWineD3D_GetAdapterCount(This->WineD3D);
74 }
75
76 HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(LPDIRECT3D9 iface, UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier) {
77     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
78     WINED3DADAPTER_IDENTIFIER adapter_id;
79
80     /* dx8 and dx9 have different structures to be filled in, with incompatible 
81        layouts so pass in pointers to the places to be filled via an internal 
82        structure                                                                */
83     adapter_id.Driver           = pIdentifier->Driver;          
84     adapter_id.Description      = pIdentifier->Description;     
85     adapter_id.DeviceName       = pIdentifier->DeviceName;      
86     adapter_id.DriverVersion    = &pIdentifier->DriverVersion;   
87     adapter_id.VendorId         = &pIdentifier->VendorId;        
88     adapter_id.DeviceId         = &pIdentifier->DeviceId;        
89     adapter_id.SubSysId         = &pIdentifier->SubSysId;        
90     adapter_id.Revision         = &pIdentifier->Revision;        
91     adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
92     adapter_id.WHQLLevel        = &pIdentifier->WHQLLevel;       
93
94     return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
95 }
96
97 UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format) {
98     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
99     return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, Format);
100 }
101
102 HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
103     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
104     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, Format, Mode, pMode);
105 }
106
107 HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
108     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
109     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, pMode);
110 }
111
112 HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(LPDIRECT3D9 iface,
113                                               UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
114                                               D3DFORMAT BackBufferFormat, BOOL Windowed) {
115     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
116     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
117                                     BackBufferFormat, Windowed);
118 }
119
120 HRESULT  WINAPI  IDirect3D9Impl_CheckDeviceFormat(LPDIRECT3D9 iface,
121                                                   UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
122                                                   DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
123     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
124     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
125                                     Usage, RType, CheckFormat);
126 }
127
128 HRESULT  WINAPI  IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9 iface,
129                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
130                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
131     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
132     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
133                                                Windowed, MultiSampleType, pQualityLevels);
134 }
135
136 HRESULT  WINAPI  IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9 iface, 
137                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
138                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
139     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
140     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
141                                            RenderTargetFormat, DepthStencilFormat);
142 }
143
144 HRESULT  WINAPI  IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
145     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
146     return IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType, SourceFormat,
147                                                 TargetFormat);
148 }
149
150 HRESULT  WINAPI  IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
151     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
152     return IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, (WINED3DCAPS *)pCaps);
153 }
154
155 HMONITOR WINAPI  IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9 iface, UINT Adapter) {
156     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
157     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
158 }
159
160 /* Internal function called back during the CreateDevice to create a render target */
161 HRESULT WINAPI D3D9CB_CreateRenderTarget(IUnknown *device, UINT Width, UINT Height, 
162                                          WINED3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
163                                          DWORD MultisampleQuality, BOOL Lockable, 
164                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
165     HRESULT res = D3D_OK;
166     IDirect3DSurface9Impl *d3dSurface = NULL;
167     IDirect3DDevice9Impl* pDeviceImpl = (IDirect3DDevice9Impl*) device;
168
169     res = IDirect3DDevice9_CreateRenderTarget((IDirect3DDevice9 *)device, Width, Height, 
170                                          (D3DFORMAT) Format, MultiSample, MultisampleQuality, Lockable, 
171                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
172
173     if (SUCCEEDED(res)) {
174         *ppSurface = d3dSurface->wineD3DSurface;
175         if (NULL == pDeviceImpl->backBuffer) {
176           pDeviceImpl->backBuffer = d3dSurface;
177           pDeviceImpl->renderTarget = d3dSurface;
178           IDirect3DSurface9Impl_AddRef((LPDIRECT3DSURFACE9) pDeviceImpl->renderTarget);
179         }
180     } else {
181         *ppSurface = NULL;
182     }
183     return res;
184 }
185
186 HRESULT  WINAPI  IDirect3D9Impl_CreateDevice(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
187                                              DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, 
188                                              IDirect3DDevice9** ppReturnedDeviceInterface) {
189
190     IDirect3D9Impl       *This   = (IDirect3D9Impl *)iface;
191     IDirect3DDevice9Impl *object = NULL;
192     WINED3DPRESENT_PARAMETERS localParameters;
193
194     /* Check the validity range of the adapter parameter */
195     if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
196         *ppReturnedDeviceInterface = NULL;
197         return D3DERR_INVALIDCALL;
198     }
199
200     /* Allocate the storage for the device object */
201     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
202     if (NULL == object) {
203         FIXME("Allocation of memory failed\n");
204         *ppReturnedDeviceInterface = NULL;
205         return D3DERR_OUTOFVIDEOMEMORY;
206     }
207
208     object->lpVtbl = &Direct3DDevice9_Vtbl;
209     object->ref = 1;
210     object->direct3d = This;
211     IDirect3D9_AddRef((LPDIRECT3D9) object->direct3d);
212     *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
213     
214     /* Allocate an associated WineD3DDevice object */
215     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
216     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;           
217     localParameters.BackBufferFormat               = &pPresentationParameters->BackBufferFormat;           
218     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;            
219     localParameters.MultiSampleType                = &pPresentationParameters->MultiSampleType;            
220     localParameters.MultiSampleQuality             = &pPresentationParameters->MultiSampleQuality;         
221     localParameters.SwapEffect                     = &pPresentationParameters->SwapEffect;                 
222     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;              
223     localParameters.Windowed                       = &pPresentationParameters->Windowed;                   
224     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;     
225     localParameters.AutoDepthStencilFormat         = &pPresentationParameters->AutoDepthStencilFormat;     
226     localParameters.Flags                          = &pPresentationParameters->Flags;                      
227     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz; 
228     localParameters.PresentationInterval           = &pPresentationParameters->PresentationInterval;       
229     return IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &localParameters, &object->WineD3DDevice, (IUnknown *)object, D3D9CB_CreateRenderTarget);
230 }
231
232 IDirect3D9Vtbl Direct3D9_Vtbl =
233 {
234     IDirect3D9Impl_QueryInterface,
235     IDirect3D9Impl_AddRef,
236     IDirect3D9Impl_Release,
237     IDirect3D9Impl_RegisterSoftwareDevice,
238     IDirect3D9Impl_GetAdapterCount,
239     IDirect3D9Impl_GetAdapterIdentifier,
240     IDirect3D9Impl_GetAdapterModeCount,
241     IDirect3D9Impl_EnumAdapterModes,
242     IDirect3D9Impl_GetAdapterDisplayMode,
243     IDirect3D9Impl_CheckDeviceType,
244     IDirect3D9Impl_CheckDeviceFormat,
245     IDirect3D9Impl_CheckDeviceMultiSampleType,
246     IDirect3D9Impl_CheckDepthStencilMatch,
247     IDirect3D9Impl_CheckDeviceFormatConversion,
248     IDirect3D9Impl_GetDeviceCaps,
249     IDirect3D9Impl_GetAdapterMonitor,
250     IDirect3D9Impl_CreateDevice
251 };