wined3d/ddraw: Forward DDSCL_MULTITHREADED to wined3d.
[wine/multimedia.git] / dlls / d3d8 / directx.c
blobe05d64722e56522d2b28e839474bb543e138ec12
1 /*
2 * IDirect3D8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
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.
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.
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
23 #include "config.h"
25 #include <stdarg.h>
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"
36 #include "d3d8_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
43 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
45 if (IsEqualGUID(riid, &IID_IUnknown)
46 || IsEqualGUID(riid, &IID_IDirect3D8)) {
47 IUnknown_AddRef(iface);
48 *ppobj = This;
49 return S_OK;
52 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
53 *ppobj = NULL;
54 return E_NOINTERFACE;
57 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
58 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
59 ULONG ref = InterlockedIncrement(&This->ref);
61 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
63 return ref;
66 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
67 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
68 ULONG ref = InterlockedDecrement(&This->ref);
70 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
72 if (ref == 0) {
73 TRACE("Releasing wined3d %p\n", This->WineD3D);
74 IWineD3D_Release(This->WineD3D);
75 HeapFree(GetProcessHeap(), 0, This);
78 return ref;
81 /* IDirect3D8 Interface follow: */
82 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
83 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
84 return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
87 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
88 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
89 return IWineD3D_GetAdapterCount(This->WineD3D);
92 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier (LPDIRECT3D8 iface,
93 UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
94 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
95 WINED3DADAPTER_IDENTIFIER adapter_id;
97 /* dx8 and dx9 have different structures to be filled in, with incompatible
98 layouts so pass in pointers to the places to be filled via an internal
99 structure */
100 adapter_id.Driver = pIdentifier->Driver;
101 adapter_id.Description = pIdentifier->Description;
102 adapter_id.DeviceName = NULL; /* d3d9 only */
103 adapter_id.DriverVersion = &pIdentifier->DriverVersion;
104 adapter_id.VendorId = &pIdentifier->VendorId;
105 adapter_id.DeviceId = &pIdentifier->DeviceId;
106 adapter_id.SubSysId = &pIdentifier->SubSysId;
107 adapter_id.Revision = &pIdentifier->Revision;
108 adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
109 adapter_id.WHQLLevel = &pIdentifier->WHQLLevel;
111 return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
114 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
115 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
116 return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
119 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
120 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
121 /* FIXME: USe a constant WINED3DFOTMAT_ANY, or something similar */
122 return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, 0 /* format */, Mode, (WINED3DDISPLAYMODE *) pMode);
125 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
126 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
127 return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
130 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface,
131 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
132 D3DFORMAT BackBufferFormat, BOOL Windowed) {
133 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
134 return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
135 BackBufferFormat, Windowed);
138 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface,
139 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
140 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
141 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
142 return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
143 Usage, RType, CheckFormat);
146 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
147 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
148 BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
149 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
150 return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
151 Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
154 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface,
155 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
156 D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
157 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
158 return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
159 RenderTargetFormat, DepthStencilFormat);
162 static HRESULT WINAPI IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
163 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
164 HRESULT hrc = D3D_OK;
165 WINED3DCAPS *pWineCaps;
167 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
169 if(NULL == 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 D3D8CAPSTOWINECAPS(pCaps, pWineCaps)
177 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
178 HeapFree(GetProcessHeap(), 0, pWineCaps);
180 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
181 if(pCaps->PixelShaderVersion > D3DPS_VERSION(1,4)){
182 pCaps->PixelShaderVersion = D3DPS_VERSION(1,4);
184 if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
185 pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
188 TRACE("(%p) returning %p\n", This, pCaps);
189 return hrc;
192 static HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
193 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
194 return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
197 /* Internal function called back during the CreateDevice to create a render target */
198 HRESULT WINAPI D3D8CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
199 WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
200 DWORD MultisampleQuality, BOOL Lockable,
201 IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
202 HRESULT res = D3D_OK;
203 IDirect3DSurface8Impl *d3dSurface = NULL;
205 TRACE("(%p) call back\n", device);
206 res = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)device, Width, Height,
207 (D3DFORMAT)Format, MultiSample, Lockable,
208 (IDirect3DSurface8 **)&d3dSurface);
210 if (SUCCEEDED(res)) {
211 *ppSurface = d3dSurface->wineD3DSurface;
212 d3dSurface->container = device;
213 d3dSurface->isImplicit = TRUE;
214 /* Implicit surfaces are created with an refcount of 0 */
215 IUnknown_Release((IUnknown *)d3dSurface);
216 } else {
217 *ppSurface = NULL;
219 return res;
222 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
223 IDirect3DSurface8Impl* surfaceParent;
224 TRACE("(%p) call back\n", pSurface);
226 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
227 surfaceParent->isImplicit = FALSE;
228 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
229 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
232 /* Callback for creating the inplicite swapchain when the device is created */
233 static HRESULT WINAPI D3D8CB_CreateAdditionalSwapChain(IUnknown *device,
234 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
235 IWineD3DSwapChain ** ppSwapChain){
236 HRESULT res = D3D_OK;
237 IDirect3DSwapChain8Impl *d3dSwapChain = NULL;
238 D3DPRESENT_PARAMETERS localParameters;
239 TRACE("(%p) call back\n", device);
241 /* Copy the presentation parameters */
242 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
243 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
244 localParameters.BackBufferFormat = pPresentationParameters->BackBufferFormat;
245 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
246 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
247 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
248 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
249 localParameters.Windowed = pPresentationParameters->Windowed;
250 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
251 localParameters.AutoDepthStencilFormat = pPresentationParameters->AutoDepthStencilFormat;
252 localParameters.Flags = pPresentationParameters->Flags;
253 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
254 localParameters.FullScreen_PresentationInterval = pPresentationParameters->PresentationInterval;
256 res = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)device, &localParameters, (IDirect3DSwapChain8 **)&d3dSwapChain);
258 /* Copy back the presentation parameters */
259 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
260 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
261 pPresentationParameters->BackBufferFormat = localParameters.BackBufferFormat;
262 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
263 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
264 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
265 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
266 pPresentationParameters->Windowed = localParameters.Windowed;
267 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
268 pPresentationParameters->AutoDepthStencilFormat = localParameters.AutoDepthStencilFormat;
269 pPresentationParameters->Flags = localParameters.Flags;
270 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
271 pPresentationParameters->PresentationInterval = localParameters.FullScreen_PresentationInterval;
273 if (SUCCEEDED(res)) {
274 *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
275 IUnknown_Release(d3dSwapChain->parentDevice);
276 d3dSwapChain->parentDevice = NULL;
277 } else {
278 *ppSwapChain = NULL;
281 return res;
284 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
285 IUnknown* swapChainParent;
286 TRACE("(%p) call back\n", pSwapChain);
288 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
289 IUnknown_Release(swapChainParent);
290 return IUnknown_Release(swapChainParent);
293 /* Internal function called back during the CreateDevice to create a render target */
294 HRESULT WINAPI D3D8CB_CreateDepthStencilSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
295 WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
296 DWORD MultisampleQuality, BOOL Discard,
297 IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
298 HRESULT res = D3D_OK;
299 IDirect3DSurface8Impl *d3dSurface = NULL;
300 TRACE("(%p) call back\n", device);
302 res = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)device, Width, Height,
303 (D3DFORMAT)Format, MultiSample, (IDirect3DSurface8 **)&d3dSurface);
304 if (SUCCEEDED(res)) {
305 *ppSurface = d3dSurface->wineD3DSurface;
306 d3dSurface->container = device;
307 d3dSurface->isImplicit = TRUE;
308 /* Implicit surfaces are created with an refcount of 0 */
309 IUnknown_Release((IUnknown *)d3dSurface);
311 return res;
314 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
315 IDirect3DSurface8Impl* surfaceParent;
316 TRACE("(%p) call back\n", pSurface);
318 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
319 surfaceParent->isImplicit = FALSE;
320 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
321 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
324 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
325 DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
326 IDirect3DDevice8** ppReturnedDeviceInterface) {
328 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
329 IDirect3DDevice8Impl *object = NULL;
330 WINED3DPRESENT_PARAMETERS localParameters;
331 HRESULT hr;
332 TRACE("(%p) Relay\n", This);
334 /* Check the validity range of the adapter parameter */
335 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
336 *ppReturnedDeviceInterface = NULL;
337 return D3DERR_INVALIDCALL;
340 /* Allocate the storage for the device object */
341 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
342 if (NULL == object) {
343 FIXME("Allocation of memory failed\n");
344 *ppReturnedDeviceInterface = NULL;
345 return D3DERR_OUTOFVIDEOMEMORY;
348 object->lpVtbl = &Direct3DDevice8_Vtbl;
349 object->ref = 1;
350 object->shader_handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, INITIAL_SHADER_HANDLE_TABLE_SIZE * sizeof(shader_handle));
351 object->shader_handle_table_size = INITIAL_SHADER_HANDLE_TABLE_SIZE;
352 *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
354 /* Allocate an associated WineD3DDevice object */
355 hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
357 if (hr != D3D_OK) {
358 HeapFree(GetProcessHeap(), 0, object);
359 *ppReturnedDeviceInterface = NULL;
360 return hr;
363 TRACE("(%p) : Created Device %p\n", This, object);
365 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
366 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
367 localParameters.BackBufferFormat = pPresentationParameters->BackBufferFormat;
368 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
369 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
370 localParameters.MultiSampleQuality = 0; /* d3d9 only */
371 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
372 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
373 localParameters.Windowed = pPresentationParameters->Windowed;
374 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
375 localParameters.AutoDepthStencilFormat = pPresentationParameters->AutoDepthStencilFormat;
376 localParameters.Flags = pPresentationParameters->Flags;
377 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
378 localParameters.PresentationInterval = pPresentationParameters->FullScreen_PresentationInterval;
380 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
381 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
384 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D8CB_CreateAdditionalSwapChain);
386 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
387 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
388 pPresentationParameters->BackBufferFormat = localParameters.BackBufferFormat;
389 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
390 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
391 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
392 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
393 pPresentationParameters->Windowed = localParameters.Windowed;
394 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
395 pPresentationParameters->AutoDepthStencilFormat = localParameters.AutoDepthStencilFormat;
396 pPresentationParameters->Flags = localParameters.Flags;
397 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
398 pPresentationParameters->FullScreen_PresentationInterval = localParameters.PresentationInterval;
400 if (hr != D3D_OK) {
401 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
402 HeapFree(GetProcessHeap(), 0, object);
403 *ppReturnedDeviceInterface = NULL;
406 return hr;
409 const IDirect3D8Vtbl Direct3D8_Vtbl =
411 /* IUnknown */
412 IDirect3D8Impl_QueryInterface,
413 IDirect3D8Impl_AddRef,
414 IDirect3D8Impl_Release,
415 /* IDirect3D8 */
416 IDirect3D8Impl_RegisterSoftwareDevice,
417 IDirect3D8Impl_GetAdapterCount,
418 IDirect3D8Impl_GetAdapterIdentifier,
419 IDirect3D8Impl_GetAdapterModeCount,
420 IDirect3D8Impl_EnumAdapterModes,
421 IDirect3D8Impl_GetAdapterDisplayMode,
422 IDirect3D8Impl_CheckDeviceType,
423 IDirect3D8Impl_CheckDeviceFormat,
424 IDirect3D8Impl_CheckDeviceMultiSampleType,
425 IDirect3D8Impl_CheckDepthStencilMatch,
426 IDirect3D8Impl_GetDeviceCaps,
427 IDirect3D8Impl_GetAdapterMonitor,
428 IDirect3D8Impl_CreateDevice