widl: Fix alignment check for struct members format string.
[wine/multimedia.git] / dlls / d3d9 / directx.c
blob3dcd9319b833f728c25652e0d6c22a033c100ce6
1 /*
2 * IDirect3D9 implementation
4 * Copyright 2002 Jason Edmeades
5 * Copyright 2005 Oliver Stieber
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 /* IDirect3D9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3D9Impl_QueryInterface(LPDIRECT3D9 iface, REFIID riid, LPVOID* ppobj)
30 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
32 if (IsEqualGUID(riid, &IID_IUnknown)
33 || IsEqualGUID(riid, &IID_IDirect3D9)) {
34 IUnknown_AddRef(iface);
35 *ppobj = This;
36 return S_OK;
39 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40 *ppobj = NULL;
41 return E_NOINTERFACE;
44 static ULONG WINAPI IDirect3D9Impl_AddRef(LPDIRECT3D9 iface) {
45 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
46 ULONG ref = InterlockedIncrement(&This->ref);
48 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50 return ref;
53 static ULONG WINAPI IDirect3D9Impl_Release(LPDIRECT3D9 iface) {
54 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
55 ULONG ref = InterlockedDecrement(&This->ref);
57 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59 if (ref == 0) {
60 IWineD3D_Release(This->WineD3D);
61 HeapFree(GetProcessHeap(), 0, This);
64 return ref;
67 /* IDirect3D9 Interface follow: */
68 static HRESULT WINAPI IDirect3D9Impl_RegisterSoftwareDevice(LPDIRECT3D9 iface, void* pInitializeFunction) {
69 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
70 return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
73 static UINT WINAPI IDirect3D9Impl_GetAdapterCount(LPDIRECT3D9 iface) {
74 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
75 return IWineD3D_GetAdapterCount(This->WineD3D);
78 static HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(LPDIRECT3D9 iface, UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier) {
79 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
80 WINED3DADAPTER_IDENTIFIER adapter_id;
82 /* dx8 and dx9 have different structures to be filled in, with incompatible
83 layouts so pass in pointers to the places to be filled via an internal
84 structure */
85 adapter_id.Driver = pIdentifier->Driver;
86 adapter_id.Description = pIdentifier->Description;
87 adapter_id.DeviceName = pIdentifier->DeviceName;
88 adapter_id.DriverVersion = &pIdentifier->DriverVersion;
89 adapter_id.VendorId = &pIdentifier->VendorId;
90 adapter_id.DeviceId = &pIdentifier->DeviceId;
91 adapter_id.SubSysId = &pIdentifier->SubSysId;
92 adapter_id.Revision = &pIdentifier->Revision;
93 adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
94 adapter_id.WHQLLevel = &pIdentifier->WHQLLevel;
96 return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
99 static UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format) {
100 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
101 return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, Format);
104 static HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
105 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
106 /* We can't pass this to WineD3D, otherwise it'll think it came from D3D8.
107 It's supposed to fail anyway, so no harm returning failure. */
108 if(Format == D3DFMT_UNKNOWN)
109 return D3DERR_INVALIDCALL;
110 return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, Format, Mode, (WINED3DDISPLAYMODE *) pMode);
113 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
114 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
115 return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
118 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(LPDIRECT3D9 iface,
119 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
120 D3DFORMAT BackBufferFormat, BOOL Windowed) {
121 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
122 return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
123 BackBufferFormat, Windowed);
126 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(LPDIRECT3D9 iface,
127 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
128 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
129 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
130 return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
131 Usage, RType, CheckFormat);
134 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9 iface,
135 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
136 BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
137 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
138 return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
139 Windowed, MultiSampleType, pQualityLevels);
142 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9 iface,
143 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
144 D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
145 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
146 return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
147 RenderTargetFormat, DepthStencilFormat);
150 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
151 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
152 return IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType, SourceFormat,
153 TargetFormat);
156 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
157 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
158 HRESULT hrc = D3D_OK;
159 WINED3DCAPS *pWineCaps;
161 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
163 if(NULL == pCaps){
164 return D3DERR_INVALIDCALL;
166 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
167 if(pWineCaps == NULL){
168 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
170 D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
171 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
172 HeapFree(GetProcessHeap(), 0, pWineCaps);
174 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
175 pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
176 TRACE("(%p) returning %p\n", This, pCaps);
177 return hrc;
180 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9 iface, UINT Adapter) {
181 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
182 return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
185 /* Internal function called back during the CreateDevice to create a render target */
186 HRESULT WINAPI D3D9CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
187 WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
188 DWORD MultisampleQuality, BOOL Lockable,
189 IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
190 HRESULT res = D3D_OK;
191 IDirect3DSurface9Impl *d3dSurface = NULL;
192 TRACE("(%p) call back\n", device);
193 res = IDirect3DDevice9_CreateRenderTarget((IDirect3DDevice9 *)device, Width, Height,
194 (D3DFORMAT)Format, MultiSample, MultisampleQuality, Lockable,
195 (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
197 if (SUCCEEDED(res)) {
198 *ppSurface = d3dSurface->wineD3DSurface;
199 d3dSurface->container = pSuperior;
200 d3dSurface->isImplicit = TRUE;
201 /* Implicit surfaces are created with an refcount of 0 */
202 IUnknown_Release((IUnknown *)d3dSurface);
203 } else {
204 *ppSurface = NULL;
206 return res;
209 ULONG WINAPI D3D9CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
210 IDirect3DSurface9Impl* surfaceParent;
211 TRACE("(%p) call back\n", pSurface);
213 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
214 surfaceParent->isImplicit = FALSE;
215 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
216 return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
219 static HRESULT WINAPI D3D9CB_CreateAdditionalSwapChain(IUnknown *device,
220 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
221 IWineD3DSwapChain ** ppSwapChain) {
222 HRESULT res = D3D_OK;
223 IDirect3DSwapChain9Impl *d3dSwapChain = NULL;
224 D3DPRESENT_PARAMETERS localParameters;
225 TRACE("(%p) call back\n", device);
227 localParameters.BackBufferWidth = *(pPresentationParameters->BackBufferWidth);
228 localParameters.BackBufferHeight = *(pPresentationParameters->BackBufferHeight);
229 localParameters.BackBufferFormat = *(pPresentationParameters->BackBufferFormat);
230 localParameters.BackBufferCount = *(pPresentationParameters->BackBufferCount);
231 localParameters.MultiSampleType = *(pPresentationParameters->MultiSampleType);
232 localParameters.MultiSampleQuality = *(pPresentationParameters->MultiSampleQuality);
233 localParameters.SwapEffect = *(pPresentationParameters->SwapEffect);
234 localParameters.hDeviceWindow = *(pPresentationParameters->hDeviceWindow);
235 localParameters.Windowed = *(pPresentationParameters->Windowed);
236 localParameters.EnableAutoDepthStencil = *(pPresentationParameters->EnableAutoDepthStencil);
237 localParameters.AutoDepthStencilFormat = *(pPresentationParameters->AutoDepthStencilFormat);
238 localParameters.Flags = *(pPresentationParameters->Flags);
239 localParameters.FullScreen_RefreshRateInHz = *(pPresentationParameters->FullScreen_RefreshRateInHz);
240 localParameters.PresentationInterval = *(pPresentationParameters->PresentationInterval);
242 /*copy the presentation parameters*/
243 res = IDirect3DDevice9_CreateAdditionalSwapChain((IDirect3DDevice9 *)device, &localParameters, (IDirect3DSwapChain9 **)&d3dSwapChain);
245 if (SUCCEEDED(res)) {
246 *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
247 d3dSwapChain->isImplicit = TRUE;
248 /* Implicit swap chains are created with an refcount of 0 */
249 IUnknown_Release((IUnknown *)d3dSwapChain);
250 } else {
251 *ppSwapChain = NULL;
253 /*Copy back the presentation parameters*/
254 *pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
255 *pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
256 *pPresentationParameters->BackBufferFormat = localParameters.BackBufferFormat;
257 *pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
258 *pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
259 *pPresentationParameters->MultiSampleQuality = localParameters.MultiSampleQuality;
260 *pPresentationParameters->SwapEffect = localParameters.SwapEffect;
261 *pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
262 *pPresentationParameters->Windowed = localParameters.Windowed;
263 *pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
264 *pPresentationParameters->AutoDepthStencilFormat = localParameters.AutoDepthStencilFormat;
265 *pPresentationParameters->Flags = localParameters.Flags;
266 *pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
267 *pPresentationParameters->PresentationInterval = localParameters.PresentationInterval;
269 return res;
272 ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
273 IDirect3DSwapChain9Impl* swapChainParent;
274 TRACE("(%p) call back\n", pSwapChain);
276 IWineD3DSwapChain_GetParent(pSwapChain,(IUnknown **) &swapChainParent);
277 swapChainParent->isImplicit = FALSE;
278 /* Swap chain had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
279 return IDirect3DSwapChain9_Release((IDirect3DSwapChain9*) swapChainParent);
282 /* Internal function called back during the CreateDevice to create a render target */
283 HRESULT WINAPI D3D9CB_CreateDepthStencilSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
284 WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
285 DWORD MultisampleQuality, BOOL Discard,
286 IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
287 HRESULT res = D3D_OK;
288 IDirect3DSurface9Impl *d3dSurface = NULL;
289 TRACE("(%p) call back\n", device);
291 res = IDirect3DDevice9_CreateDepthStencilSurface((IDirect3DDevice9 *)device, Width, Height,
292 (D3DFORMAT)Format, MultiSample, MultisampleQuality, Discard,
293 (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
294 if (SUCCEEDED(res)) {
295 *ppSurface = d3dSurface->wineD3DSurface;
296 d3dSurface->container = device;
297 d3dSurface->isImplicit = TRUE;
298 /* Implicit surfaces are created with an refcount of 0 */
299 IUnknown_Release((IUnknown *)d3dSurface);
301 return res;
304 ULONG WINAPI D3D9CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
305 IDirect3DSurface9Impl* surfaceParent;
306 TRACE("(%p) call back\n", pSurface);
308 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
309 surfaceParent->isImplicit = FALSE;
310 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
311 return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
314 static HRESULT WINAPI IDirect3D9Impl_CreateDevice(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType,
315 HWND hFocusWindow, DWORD BehaviourFlags,
316 D3DPRESENT_PARAMETERS* pPresentationParameters,
317 IDirect3DDevice9** ppReturnedDeviceInterface) {
319 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
320 IDirect3DDevice9Impl *object = NULL;
321 WINED3DPRESENT_PARAMETERS localParameters;
322 HRESULT hr;
323 TRACE("(%p) Relay\n", This);
325 /* Check the validity range of the adapter parameter */
326 if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
327 *ppReturnedDeviceInterface = NULL;
328 return D3DERR_INVALIDCALL;
331 /* Allocate the storage for the device object */
332 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
333 if (NULL == object) {
334 FIXME("Allocation of memory failed\n");
335 *ppReturnedDeviceInterface = NULL;
336 return D3DERR_OUTOFVIDEOMEMORY;
339 object->lpVtbl = &Direct3DDevice9_Vtbl;
340 object->ref = 1;
341 *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
343 /* Allocate an associated WineD3DDevice object */
344 localParameters.BackBufferWidth = &pPresentationParameters->BackBufferWidth;
345 localParameters.BackBufferHeight = &pPresentationParameters->BackBufferHeight;
346 localParameters.BackBufferFormat = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
347 localParameters.BackBufferCount = &pPresentationParameters->BackBufferCount;
348 localParameters.MultiSampleType = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
349 localParameters.MultiSampleQuality = &pPresentationParameters->MultiSampleQuality;
350 localParameters.SwapEffect = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
351 localParameters.hDeviceWindow = &pPresentationParameters->hDeviceWindow;
352 localParameters.Windowed = &pPresentationParameters->Windowed;
353 localParameters.EnableAutoDepthStencil = &pPresentationParameters->EnableAutoDepthStencil;
354 localParameters.AutoDepthStencilFormat = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
355 localParameters.Flags = &pPresentationParameters->Flags;
356 localParameters.FullScreen_RefreshRateInHz = &pPresentationParameters->FullScreen_RefreshRateInHz;
357 localParameters.PresentationInterval = &pPresentationParameters->PresentationInterval;
359 hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
361 if (hr != D3D_OK) {
362 HeapFree(GetProcessHeap(), 0, object);
363 *ppReturnedDeviceInterface = NULL;
364 return hr;
367 TRACE("(%p) : Created Device %p\n", This, object);
369 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D9CB_CreateAdditionalSwapChain);
370 if (hr != D3D_OK) {
371 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
372 HeapFree(GetProcessHeap(), 0, object);
373 *ppReturnedDeviceInterface = NULL;
375 return hr;
380 const IDirect3D9Vtbl Direct3D9_Vtbl =
382 /* IUnknown */
383 IDirect3D9Impl_QueryInterface,
384 IDirect3D9Impl_AddRef,
385 IDirect3D9Impl_Release,
386 /* IDirect3D9 */
387 IDirect3D9Impl_RegisterSoftwareDevice,
388 IDirect3D9Impl_GetAdapterCount,
389 IDirect3D9Impl_GetAdapterIdentifier,
390 IDirect3D9Impl_GetAdapterModeCount,
391 IDirect3D9Impl_EnumAdapterModes,
392 IDirect3D9Impl_GetAdapterDisplayMode,
393 IDirect3D9Impl_CheckDeviceType,
394 IDirect3D9Impl_CheckDeviceFormat,
395 IDirect3D9Impl_CheckDeviceMultiSampleType,
396 IDirect3D9Impl_CheckDepthStencilMatch,
397 IDirect3D9Impl_CheckDeviceFormatConversion,
398 IDirect3D9Impl_GetDeviceCaps,
399 IDirect3D9Impl_GetAdapterMonitor,
400 IDirect3D9Impl_CreateDevice