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
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);
39 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
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);
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);
60 IWineD3D_Release(This->WineD3D);
61 HeapFree(GetProcessHeap(), 0, This);
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
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;
102 /* Others than that not supported by d3d9, but reported by wined3d for ddraw. Filter them out */
103 if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5) {
107 return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, Format);
110 static HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
111 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
112 /* We can't pass this to WineD3D, otherwise it'll think it came from D3D8 or DDraw.
113 It's supposed to fail anyway, so no harm returning failure. */
114 if(Format != WINED3DFMT_X8R8G8B8 && Format != WINED3DFMT_R5G6B5)
115 return D3DERR_INVALIDCALL;
116 return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, Format, Mode, (WINED3DDISPLAYMODE *) pMode);
119 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
120 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
121 return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
124 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(LPDIRECT3D9 iface,
125 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
126 D3DFORMAT BackBufferFormat, BOOL Windowed) {
127 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
128 return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
129 BackBufferFormat, Windowed);
132 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(LPDIRECT3D9 iface,
133 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
134 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
135 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
136 return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
137 Usage, RType, CheckFormat);
140 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9 iface,
141 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
142 BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
143 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
144 return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
145 Windowed, MultiSampleType, pQualityLevels);
148 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9 iface,
149 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
150 D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
151 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
152 return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
153 RenderTargetFormat, DepthStencilFormat);
156 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
157 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
158 return IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType, SourceFormat,
162 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
163 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
164 HRESULT hrc = D3D_OK;
165 WINED3DCAPS *pWineCaps;
167 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
170 return D3DERR_INVALIDCALL;
172 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
173 if(pWineCaps == NULL){
174 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
176 D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
177 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
178 HeapFree(GetProcessHeap(), 0, pWineCaps);
180 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
181 pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
182 TRACE("(%p) returning %p\n", This, pCaps);
186 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9 iface, UINT Adapter) {
187 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
188 return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
191 /* Internal function called back during the CreateDevice to create a render target */
192 HRESULT WINAPI D3D9CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
193 WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
194 DWORD MultisampleQuality, BOOL Lockable,
195 IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
196 HRESULT res = D3D_OK;
197 IDirect3DSurface9Impl *d3dSurface = NULL;
198 TRACE("(%p) call back\n", device);
199 res = IDirect3DDevice9_CreateRenderTarget((IDirect3DDevice9 *)device, Width, Height,
200 (D3DFORMAT)Format, MultiSample, MultisampleQuality, Lockable,
201 (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
203 if (SUCCEEDED(res)) {
204 *ppSurface = d3dSurface->wineD3DSurface;
205 d3dSurface->container = pSuperior;
206 d3dSurface->isImplicit = TRUE;
207 /* Implicit surfaces are created with an refcount of 0 */
208 IUnknown_Release((IUnknown *)d3dSurface);
215 ULONG WINAPI D3D9CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
216 IDirect3DSurface9Impl* surfaceParent;
217 TRACE("(%p) call back\n", pSurface);
219 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
220 surfaceParent->isImplicit = FALSE;
221 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
222 return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
225 static HRESULT WINAPI D3D9CB_CreateAdditionalSwapChain(IUnknown *device,
226 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
227 IWineD3DSwapChain ** ppSwapChain) {
228 HRESULT res = D3D_OK;
229 IDirect3DSwapChain9Impl *d3dSwapChain = NULL;
230 D3DPRESENT_PARAMETERS localParameters;
231 TRACE("(%p) call back\n", device);
233 /* Copy the presentation parameters */
234 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
235 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
236 localParameters.BackBufferFormat = pPresentationParameters->BackBufferFormat;
237 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
238 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
239 localParameters.MultiSampleQuality = pPresentationParameters->MultiSampleQuality;
240 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
241 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
242 localParameters.Windowed = pPresentationParameters->Windowed;
243 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
244 localParameters.AutoDepthStencilFormat = pPresentationParameters->AutoDepthStencilFormat;
245 localParameters.Flags = pPresentationParameters->Flags;
246 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
247 localParameters.PresentationInterval = pPresentationParameters->PresentationInterval;
249 res = IDirect3DDevice9_CreateAdditionalSwapChain((IDirect3DDevice9 *)device, &localParameters, (IDirect3DSwapChain9 **)&d3dSwapChain);
251 if (SUCCEEDED(res)) {
252 *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
253 d3dSwapChain->isImplicit = TRUE;
254 /* Implicit swap chains are created with an refcount of 0 */
255 IUnknown_Release((IUnknown *)d3dSwapChain);
260 /* Copy back the presentation parameters */
261 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
262 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
263 pPresentationParameters->BackBufferFormat = localParameters.BackBufferFormat;
264 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
265 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
266 pPresentationParameters->MultiSampleQuality = localParameters.MultiSampleQuality;
267 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
268 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
269 pPresentationParameters->Windowed = localParameters.Windowed;
270 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
271 pPresentationParameters->AutoDepthStencilFormat = localParameters.AutoDepthStencilFormat;
272 pPresentationParameters->Flags = localParameters.Flags;
273 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
274 pPresentationParameters->PresentationInterval = localParameters.PresentationInterval;
279 ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
280 IDirect3DSwapChain9Impl* swapChainParent;
281 TRACE("(%p) call back\n", pSwapChain);
283 IWineD3DSwapChain_GetParent(pSwapChain,(IUnknown **) &swapChainParent);
284 swapChainParent->isImplicit = FALSE;
285 /* Swap chain had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
286 return IDirect3DSwapChain9_Release((IDirect3DSwapChain9*) swapChainParent);
289 /* Internal function called back during the CreateDevice to create a render target */
290 HRESULT WINAPI D3D9CB_CreateDepthStencilSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
291 WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
292 DWORD MultisampleQuality, BOOL Discard,
293 IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
294 HRESULT res = D3D_OK;
295 IDirect3DSurface9Impl *d3dSurface = NULL;
296 TRACE("(%p) call back\n", device);
298 res = IDirect3DDevice9_CreateDepthStencilSurface((IDirect3DDevice9 *)device, Width, Height,
299 (D3DFORMAT)Format, MultiSample, MultisampleQuality, Discard,
300 (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
301 if (SUCCEEDED(res)) {
302 *ppSurface = d3dSurface->wineD3DSurface;
303 d3dSurface->container = device;
304 d3dSurface->isImplicit = TRUE;
305 /* Implicit surfaces are created with an refcount of 0 */
306 IUnknown_Release((IUnknown *)d3dSurface);
311 ULONG WINAPI D3D9CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
312 IDirect3DSurface9Impl* surfaceParent;
313 TRACE("(%p) call back\n", pSurface);
315 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
316 surfaceParent->isImplicit = FALSE;
317 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
318 return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
321 static HRESULT WINAPI IDirect3D9Impl_CreateDevice(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType,
322 HWND hFocusWindow, DWORD BehaviourFlags,
323 D3DPRESENT_PARAMETERS* pPresentationParameters,
324 IDirect3DDevice9** ppReturnedDeviceInterface) {
326 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
327 IDirect3DDevice9Impl *object = NULL;
328 WINED3DPRESENT_PARAMETERS localParameters;
330 TRACE("(%p) Relay\n", This);
332 /* Check the validity range of the adapter parameter */
333 if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
334 *ppReturnedDeviceInterface = NULL;
335 return D3DERR_INVALIDCALL;
338 /* Allocate the storage for the device object */
339 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
340 if (NULL == object) {
341 FIXME("Allocation of memory failed\n");
342 *ppReturnedDeviceInterface = NULL;
343 return D3DERR_OUTOFVIDEOMEMORY;
346 object->lpVtbl = &Direct3DDevice9_Vtbl;
348 *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
350 /* Allocate an associated WineD3DDevice object */
351 hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
354 HeapFree(GetProcessHeap(), 0, object);
355 *ppReturnedDeviceInterface = NULL;
359 TRACE("(%p) : Created Device %p\n", This, object);
361 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
362 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
363 localParameters.BackBufferFormat = pPresentationParameters->BackBufferFormat;
364 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
365 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
366 localParameters.MultiSampleQuality = pPresentationParameters->MultiSampleQuality;
367 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
368 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
369 localParameters.Windowed = pPresentationParameters->Windowed;
370 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
371 localParameters.AutoDepthStencilFormat = pPresentationParameters->AutoDepthStencilFormat;
372 localParameters.Flags = pPresentationParameters->Flags;
373 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
374 localParameters.PresentationInterval = pPresentationParameters->PresentationInterval;
376 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
377 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
380 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D9CB_CreateAdditionalSwapChain);
382 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
383 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
384 pPresentationParameters->BackBufferFormat = localParameters.BackBufferFormat;
385 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
386 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
387 pPresentationParameters->MultiSampleQuality = localParameters.MultiSampleQuality;
388 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
389 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
390 pPresentationParameters->Windowed = localParameters.Windowed;
391 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
392 pPresentationParameters->AutoDepthStencilFormat = localParameters.AutoDepthStencilFormat;
393 pPresentationParameters->Flags = localParameters.Flags;
394 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
395 pPresentationParameters->PresentationInterval = localParameters.PresentationInterval;
398 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
399 HeapFree(GetProcessHeap(), 0, object);
400 *ppReturnedDeviceInterface = NULL;
403 /* Initialize the converted declaration array. This creates a valid pointer and when adding decls HeapReAlloc
404 * can be used without further checking
406 object->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
413 const IDirect3D9Vtbl Direct3D9_Vtbl =
416 IDirect3D9Impl_QueryInterface,
417 IDirect3D9Impl_AddRef,
418 IDirect3D9Impl_Release,
420 IDirect3D9Impl_RegisterSoftwareDevice,
421 IDirect3D9Impl_GetAdapterCount,
422 IDirect3D9Impl_GetAdapterIdentifier,
423 IDirect3D9Impl_GetAdapterModeCount,
424 IDirect3D9Impl_EnumAdapterModes,
425 IDirect3D9Impl_GetAdapterDisplayMode,
426 IDirect3D9Impl_CheckDeviceType,
427 IDirect3D9Impl_CheckDeviceFormat,
428 IDirect3D9Impl_CheckDeviceMultiSampleType,
429 IDirect3D9Impl_CheckDepthStencilMatch,
430 IDirect3D9Impl_CheckDeviceFormatConversion,
431 IDirect3D9Impl_GetDeviceCaps,
432 IDirect3D9Impl_GetAdapterMonitor,
433 IDirect3D9Impl_CreateDevice